本文共 11376 字,大约阅读时间需要 37 分钟。
1.xml解析
xml文件解析有DOM, SAX方式。
DOM解析: 是相当于把整个xml结构以树形结构读进内存中。 优点:很方便实现增删改操作 缺点:如果文件过大,造成内存溢出 SAX解析: 采用事件驱动,边读边解析。 优点:如果文件过大,不会造成内存溢出,方便实现查询操作 缺点:不能实现增删改操作2.使用Xerces库实现xml解析
Xerces库的下载,编译等参考博文:3.Xerces库使用说明(DOM解析)
红色的叫标签,蓝色的是标签的属性。1.根据某个标签,获取整个标签中的内容。
XercesDOMParser* parser = new XercesDOMParser();const char* xmlFile = "C:\\Users\\tianyanbin\\source\\repos\\MyXml\\MyXml\\test.xml";//绝对路径parser->parse(xmlFile);DOMDocument* doc = parser->getDocument();DOMElement* m_pRoot = doc->getDocumentElement(); //获取XML根节点DOMNodeList* pVersionEleList = m_pRoot->getElementsByTagName(XMLString::transcode("Sector"));//获取Sector节点
2.获取便签中某个属性的值
DOMNode* child001 = XXXX; //chilid001是某个结点,对应一个标签对象DOMNamedNodeMap* pAttributes = child001->getAttributes(); //----------------取属性表const XMLCh* attrDef = XMLString::transcode("ProcessParameter"); //----------------把属性名字符串转变一下存起来;DOMNode* node = pAttributes->getNamedItem(attrDef); //---------------- - 根据属性名取出属性XMLString::release((XMLCh**)(&attrDef)); //----------------记得和XMLString::transcode配对使用,否则内存泄露char* pc = XMLString::transcode(node->getNodeValue()); // ----------------把属性转变为char*cout << "ProcessParameter:" << pc << endl;XMLString::release(&pc);//realse和
完成项目代码和xml文件:
test.xml:
0 100 0 0 193.784 27.234 0.000 193.731 26.621 -0.001 193.414 21.899 -0.011 193.189 17.245 -0.021 193.052 12.596 -0.031 193.004 7.983 -0.041 193.046 3.055 -0.052 193.131 0.000 -0.058 195.168 4.852 -0.058 196.265 4.867 -0.061 196.252 8.144 -0.068 195.153 8.148 -0.070 195.168 4.852 -0.077 196.253 8.544 -0.077 196.288 11.821 -0.084 195.189 11.837 -0.087 195.154 8.548 -0.094 196.253 8.544 -0.096 195.236 1.160 -0.096 196.335 1.188 -0.098 196.271 4.467 -0.105 195.172 4.452 -0.108 195.236 1.160 -0.115 196.297 12.220 -0.115 196.378 15.495 -0.122 195.278 15.532 -0.124 195.197 12.236 -0.131 196.297 12.220 -0.134 196.372 0.000 -0.134 196.345 0.788 -0.135 195.246 0.760 -0.138 195.272 0.000 -0.139 196.393 15.894 -0.139 196.524 19.171 -0.146 195.426 19.221 -0.149 195.293 15.931 -0.156 196.393 15.894 -0.158 196.545 19.568 -0.158 196.725 22.840 -0.165 195.627 22.906 -0.168 195.445 19.620 -0.175 196.545 19.568 -0.177 196.750 23.237 -0.177 196.978 26.504 -0.184 195.883 26.588 -0.186 195.653 23.305 -0.193 196.750 23.237 -0.196 195.962 27.540 -0.196 195.915 26.985 -0.197 197.011 26.902 -0.199 197.079 27.697 -0.199 197.011 26.902 -0.201 199.269 28.005 -0.201 199.003 24.686 -0.208 198.709 19.796 -0.219 198.513 14.951 -0.229 198.403 8.246 -0.243 198.437 3.560 -0.253 198.533 0.000 -0.261 193.784 27.234 0.000 193.731 26.621 -0.001 193.414 21.899 -0.011 193.189 17.245 -0.021 193.052 12.596 -0.031 193.004 7.983 -0.041 193.046 3.055 -0.052 193.131 0.000 -0.058 1 193.784 27.234 0.000 193.731 26.621 -0.001 193.414 21.899 -0.011 193.189 17.245 -0.021 193.052 12.596 -0.031 193.004 7.983 -0.041 193.046 3.055 -0.052 193.131 0.000 -0.058 195.168 4.852 -0.058 196.265 4.867 -0.061 196.252 8.144 -0.068 195.153 8.148 -0.070 195.168 4.852 -0.077 196.253 8.544 -0.077 196.288 11.821 -0.084 195.189 11.837 -0.087 195.154 8.548 -0.094 196.253 8.544 -0.096 195.236 1.160 -0.096 196.335 1.188 -0.098 196.271 4.467 -0.105 195.172 4.452 -0.108 195.236 1.160 -0.115 196.297 12.220 -0.115 196.378 15.495 -0.122 195.278 15.532 -0.124 195.197 12.236 -0.131 196.297 12.220 -0.134 196.372 0.000 -0.134 196.345 0.788 -0.135 195.246 0.760 -0.138 195.272 0.000 -0.139 196.393 15.894 -0.139 196.524 19.171 -0.146 195.426 19.221 -0.149 195.293 15.931 -0.156 196.393 15.894 -0.158 196.545 19.568 -0.158 196.725 22.840 -0.165 195.627 22.906 -0.168 195.445 19.620 -0.175 196.545 19.568 -0.177 196.750 23.237 -0.177 196.978 26.504 -0.184 195.883 26.588 -0.186 195.653 23.305 -0.193 196.750 23.237 -0.196 195.962 27.540 -0.196 195.915 26.985 -0.197 197.011 26.902 -0.199 197.079 27.697 -0.199 197.011 26.902 -0.201 199.269 28.005 -0.201 199.003 24.686 -0.208 198.709 19.796 -0.219 198.513 14.951 -0.229 198.403 8.246 -0.243 198.437 3.560 -0.253 198.533 0.000 -0.261 193.784 27.234 0.000 193.731 26.621 -0.001 193.414 21.899 -0.011 193.189 17.245 -0.021 193.052 12.596 -0.031 193.004 7.983 -0.041 193.046 3.055 -0.052 193.131 0.000 -0.059
代码:MyXml.cpp
#include#include #include #include #include #include #include #include #include #include //istringstream 必须包含这个头文件// Other include files, declarations, and non-Xerces-C++ initializations.#pragma comment (lib,"xerces-c_3D.lib")using namespace xercesc;using namespace std;string trim(string s) { if (s.empty()) return s; s.erase(0, s.find_first_not_of(" ")); s.erase(s.find_last_not_of(" ") + 1); return s;}typedef struct DataBlock { int SectorID; int GunID; int ProcessParameter; //vector里面存储的是一个完整DataBlock vector > DataBlock;}datablock;//结构体中含有string要使用new分配内存,不能使用mallocint main(){ try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { // Do your failure processing here cout << "Initialize Error!" << endl; return 1; } // Do your actual work with Xerces-C++ here. XercesDOMParser* parser = new XercesDOMParser(); parser->setValidationScheme(XercesDOMParser::Val_Always); parser->setDoNamespaces(true); // optional ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase(); parser->setErrorHandler(errHandler); //此处请用绝对路径 C:\\Users\\tianyanbin\\source\\repos\\MyXml\\MyXml\\book.xml personal.xml 0.xml job.xml //相对路径/book.xml const char* xmlFile = "C:\\Users\\tianyanbin\\source\\repos\\MyXml\\MyXml\\test.xml"; try { parser->parse(xmlFile); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return -1; } catch (const DOMException& toCatch) { char* message = XMLString::transcode(toCatch.msg); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return -2; } catch (const SAXException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return -3; } catch (...) { cout << "Unexpected Exception \n"; return -4; } //读取 DOMDocument* doc = parser->getDocument(); DOMNodeList* nodelist = doc->getChildNodes(); DOMElement* m_pRoot = doc->getDocumentElement(); //获取XML根节点 DOMNode* root = (DOMNode*)doc->getDocumentElement(); DOMNodeList* childlist; DOMNodeList* childlistProcessParameterSets; DOMNode* childProcessParameterSets; DOMNode* childParameterSet; DOMNode* Parameter; DOMNode* Parameter1; DOMNodeList* childParameter1; DOMNode* Parameter11; DOMNode* Parameter2; DOMNodeList* childParameter2; DOMNode* Parameter22; DOMNodeList* Parameters; //头文件#include ofstream out("./data.txt", ios::app);//app表示每次操作前均定位到文件末尾 if (out.fail()) { cout << "error\n"; } //out << nodename << endl; vector db; if (m_pRoot) { DOMNodeList* pVersionEleList = m_pRoot->getElementsByTagName(XMLString::transcode("Sector"));//获取Sector节点 // //cout << XMLString::transcode(m_pRoot->getTextContent()); //out << XMLString::transcode(m_pRoot->getTextContent()) << endl; for (int i = 0; i < pVersionEleList->getLength(); ++i) { //每个i 对应一个Sector,一个node1就是一个Sector节点 DOMNode* node1 = pVersionEleList->item(i); DOMNode* child = 0; datablock p; int m = 0;//记录那个枪,0,1,2,3,循环往复 for (child = node1->getFirstChild(); child != 0; child = child->getNextSibling()) { //Node node = childNodes.item(i); //int nodeType = node.getNodeType(); nodeType为3时,为#text //if (XMLString::transcode(child->getNodeName()) != "#text")//必须先对XMLString::transcode(child->getNodeName())去空格 if (child->getNodeType() != 3) { if(trim(XMLString::transcode(child->getNodeName()) )=="Id")//Sector-ID { p.SectorID = stoi(XMLString::transcode(child->getTextContent())); //m++; cout << XMLString::transcode(child->getNodeName()) << ":" << XMLString::transcode(child->getTextContent());//注:这里使用child->getNodeValue()报错 } if (trim(XMLString::transcode(child->getNodeName())) == "Gun")//有四个枪,每解析完四个枪,写一次vector,下次就是下一个Sector扇区 { if (m == 0)//第一个电子枪 { DOMNode* child001 = 0; for (child001 = child->getFirstChild(); child001 != 0; child001 = child001->getNextSibling()) { if (trim(XMLString::transcode(child001->getNodeName())) == "Id")//Gun-ID { p.GunID = stoi(XMLString::transcode(child->getTextContent())); cout << XMLString::transcode(child001->getNodeName()) << ":" << XMLString::transcode(child001->getTextContent());//注:这里使用child->getNodeValue()报错 } //解析DataBlock,每个枪里面不知道有多少个DataBlock,来一个加一个 if (trim(XMLString::transcode(child001->getNodeName())) == "DataBlock") { //获取DataBlock标签的属性值attribute DOMNamedNodeMap* pAttributes = child001->getAttributes(); //----------------取属性表 const XMLCh* attrDef = XMLString::transcode("ProcessParameter"); //----------------把属性名字符串转变一下存起来; DOMNode* node = pAttributes->getNamedItem(attrDef); //---------------- - 根据属性名取出属性 XMLString::release((XMLCh**)(&attrDef)); //----------------记得和XMLString::transcode配对使用,否则内存泄露 char* pc = XMLString::transcode(node->getNodeValue()); // ----------------把属性转变为char* p.ProcessParameter = stoi(XMLString::transcode(node->getNodeValue())); cout << "ProcessParameter:" << pc << endl; XMLString::release(&pc); DOMNode* child002 = 0; //解析PolyLine,每个DataBlock里面不知道有多少个PolyLine for (child002 = child001->getFirstChild(); child002 != 0; child002 = child002->getNextSibling()) { vector v; if (trim(XMLString::transcode(child002->getNodeName())) == "PolyLine") { //这里必须调用个函数:其功能是把数字字符串转化为数字接收 string s = "05 15 25"; istringstream ss(XMLString::transcode(child002->getTextContent())); //vector v; float i; while (ss >> i) { v.push_back(i); } p.DataBlock.push_back(v); cout << "打印vector内容" << endl; copy(v.begin(), v.end(), ostream_iterator (cout, " ")); //p.ID = stoi(XMLString::transcode(child->getTextContent())); cout << XMLString::transcode(child002->getNodeName()) << ":" << XMLString::transcode(child002->getTextContent());//注:这里使用child->getNodeValue()报错 } } } } } } } } } } out.close();//关闭文件 int stop; cin >> stop; delete parser; delete errHandler; XMLPlatformUtils::Terminate(); // Other terminations and cleanup. return 0;}
代码功能:
把test.xml文件中的信息解析出来,存到vector里面。
同时,在控制台打印出信息。 也可以把解析出的信息存入txt文件中,这部分代码里也有,但没写完整。
Refferences:
1. 2.转载地址:http://uoxai.baihongyu.com/