C++读取XML,tinyXml的使用
[来源] 达内 [编辑] 达内 [时间]2012-11-09
最近在开发的过程中,有个需求是对xml进行格式转化,从一种格式转化到另外一种格式.因此,就需要读取xml进行处理.原本打算写成工具在linux下运行,不过后来考虑到和系统结合,最后也就使用了前台js转了.反正都是读取xml,什么技术转不都是一样的么
前言:
< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 最近在开发的过程中,有个需求是对xml进行格式转化,从一种格式转化到另外一种格式.因此,就需要读取xml进行处理.原本打算写成工具在linux下运行,不过后来考虑到和系统结合,最后也就使用了前台js转了.反正都是读取xml,什么技术转不都是一样的么?< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 不过刚开始还是对要使用的技术做了一定的探究.c++要读取xml有很多种方式.比较又名的有: < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> rapidXML(这个是网上介绍的,没用过) < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> Xerces-C++ XML Parser :通常来说,读取XML的方法都是将整个文本进行读取,然后构建成DOM Tree,之后进行遍历等操作.这个Parser除了支持构建DOM Tree的方式之外,还支持类似于回调函数的方式进行处理(SAX,SAX2).在读到相应的节点然后调用函数进行处理.DOM Tree的方式好处是简单,操作很方便,但是劣势也是很明显:需要把整个XML读进之后才能做处理.如果XML很大,那内存就支撑不住了.SAX,SAX2的作用就在此.其可以支持很大的XML,因为其是相当于事件式的处理方式,不需要构建DOM Tree.不过就是比较麻烦.< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> TinyXML: 这个就是接下来要介绍的库了.取名Tiny,意在编写一个轻量级的处理基本XML的工具.因此,其支持的特性有限.下面列出了其不支持的功能: < blockquote style="margin: 10px 0px 10px 25px; padding: 10px 60px 5px; background-image: url(); background-color: rgb(255, 255, 255); border: 2px solid rgb(239, 239, 239); color: rgb(51, 51, 51); min-height: 35px; line-height: 1.6em; font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 25px 0px; background-repeat: no-repeat no-repeat; "> TinyXML doesn't parse or use DTDs (Document Type Definitions) or XSLs (eXtensible Stylesheet Language.)
-------------------官方说明文档
< p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 其给出的理由也很明确: < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 1. 支持这两个特性使得库更为庞大 < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 2. 使用更为复杂 < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 3. 学习曲线更曲折 < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 需要使用这些特性的可以考虑上面的xerces-C++ XML Parser.很强大.当然也复杂得多 < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">TinyXml使用: < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 前言说的已经够多了,咱们就不废话了,直接讲解tinyXml的使用方法. < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 安装: < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> TinyXml的安装方法并不复杂,或者说没有安装这个步骤. < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 1. 下载tinyXml ,下载tinyxml_2_6_ < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 2. 解压unzip tinyxml_2_6_.(windows就直接解压就可以了) < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 对于有vs的朋友来说,直接打开tinyxml.sln就可以了.对于linux用户来说,则需要拷贝上面全出来的6个文件. < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> PS.顺带的,我觉得可以把它的Makefile也拷出来.写得非常不错.可以参考参考. < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 使用: < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> tinyxml的使用方法很简单,只需要把拷出来的文件放在自己编写的目录下面,引用tinyxml.h,tinystr.h即可. < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 简单示例代码: < p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> < div style="margin: 5px 0px; padding: 5px; background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(68, 68, 68); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">1 #include "tinyxml.h " 2 #include " tinystr.h" 3 #include <iostream> 4 5 using namespace std; 6 int main(int argc, char *argv[]) 7 { 8 if(argc != 2 ) 9 { 10 cout << "usage: " <<argv[0] << " xmlfile" << endl; 11 return 1 ; 12 } 13 TiXmlDocument doc(argv[1 ]); 14 bool loadOk = doc.LoadFile(); 15 if (!loadOk) 16 { 17 cout << " could load:" << doc.ErrorDesc() << endl; 18 } 19 TiXmlPrinter printer;// 提供的工具类,目的是将xml的数据按格式输出20 doc.Accept(& printer); 21 cout << printer.CStr() << endl;// 输出22 23 TiXmlElement*node = doc.FirstChildElement();// 获取第一个element节点 24 cout << node->Value() << endl;// 输出节点的值25 string t; 26 node->QueryValueAttribute(" type ", &t);// 获取节点属性 27 cout << t << endl; 28 29 doc.FirstChild()->NextSibling()->ToElement()->QueryStringAttribute(" type ", &t);// 获取第二个子节点的数据 30 cout << "2:" << t << endl; 31 32 // 使用遍历的方式进行处理 33 TiXmlNode* child = NULL; 34 TiXmlElement* element = NULL; 35 TiXmlAttribute *attr = NULL; 36 int ct; 37 while (child = doc.FirstChild()-> IterateChildren(child)) 38 { 39 cout << child->ValueStr() << " \t "; 40 ct = child->Type(); 41 cout << ct << " \t" ; 42 //根据不同的节点类型做相应处理 43 switch (ct) 44 { 45 case TiXmlNoTINYXML_TEXT: 46 break ; 47 case TiXmlNoTINYXML_ELEMENT: 48 element = child->ToElement(); 49 attr = element->FirstAttribute(); 50 while (attr) 51 { 52 cout << attr->NameTStr() << "=" << attr->ValueStr() << ' \t '; 53 attr = attr->Next(); 54 } 55 break ; 56 } 57 } 58 59 return 0; 60 }
g++ -c tinyxmlparser.cpp - DTIXML_USE_STL g++ -c tinyxmlerror.cpp - DTIXML_USE_STL g++ -c tinystr.cpp - DTIXML_USE_STL g++ -c tinyxml.cpp -DTIXML_USE_STL g ++ -c xmltest.cpp -DTIXML_USE_STL g++ -o xmltest xmltest.o tinyxmlparser.o tinyxmlerror.o tinystr.o tinyxml.o
< xml> <tag1 type='1' /> <tag2 type='2' /> </xml >
<xml> <tag1 type="1 " /> <tag2 type="2 " /> </xml> xml 1 : tag1 1 type=1 tag2 1 type=2