如何解析一个xml文件呢,我们可以使用libxml2库。
libxml2解析xml文件的要点是需要理解xml文档的树形结构,弄懂三个指针root,children及next,其分别代表父、子及同胞关系。
安装libxml2:
依赖:需要安装python-devel ——yuminstall python-devel
安装libxml2——./configure; make; make install
主要函数:
xmlDocPtr xmlParseFile(const char *filename);
xmlDocPtr xmlParseMemory(const char *buffer,int size);
xmlNodePtr xmlDocGetRootElement(constxmlDoc* doc);
xmlChar * xmlNodeGetContent(const xmlNode*cur);
xmlChar * xmlGetProp(const xmlNode *node,const xmlChar *name);
xmlFree(xmlNodePtr)
xmlFreeDoc(xmlDocPtr)
实例:testXml.cpp
#include <iostream> #include <libxml/parser.h> #include <string> using namespace std; void parseXmlNode(const xmlNodePtr & xmlRootNode); const char* xmlFilename = "bookstore.xml"; int main(){ xmlDocPtr doc = xmlParseFile(xmlFilename); if(NULL == doc) { cout << "Parsed xmlFile Failed!" << endl; return -1; } xmlNodePtr xmlRootNode = xmlDocGetRootElement(doc); if(NULL == xmlRootNode) { cout << "GetRootElement Failed" << endl; xmlFreeDoc(doc); return -1; } parseXmlNode(xmlRootNode); xmlFreeDoc(doc); return 0; } void parseXmlNode(const xmlNodePtr & xmlRootNode) { xmlNodePtr xmlChildNode = xmlRootNode->xmlChildrenNode; while(NULL != xmlChildNode) { if(xmlChildNode->xmlChildrenNode != NULL) { cout << "" << (char *)xmlChildNode->name << ":"; cout << "\t" << (char*)xmlNodeGetContent(xmlChildNode) << endl; } if (!xmlStrcmp(xmlChildNode->name,(const xmlChar*)"title")) { cout << "lang of " << (char *)xmlChildNode->name << " is " << (char *)xmlGetProp(xmlChildNode,(const xmlChar*)"lang") << endl; } if (xmlChildNode->xmlChildrenNode != NULL) { parseXmlNode(xmlChildNode); } xmlChildNode = xmlChildNode->next; } return ; }
编译:
g++ testXml.cpp -I/usr/local/libxml2/include/libxml2/ -lxml2
其中/usr/local/libxml2/include/libxml2/指出了libxml文件夹的路径
输出:
book: Everyday Italian Giada De Laurentiis 2005 30.00 title: Everyday Italian lang of title is en author: Giada De Laurentiis year: 2005 price: 30.00 book: Harry Potter J K. Rowling 2005 29.99 title: Harry Potter lang of title is en author: J K. Rowling year: 2005 price: 29.99
其中bookstore.xml 如下:
<bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <bookstore>