1、xml默认是生成执行文件,要想生成动态库需要修改makefile
从http://ncu.dl.sourceforge.net/sourceforge/tinyxml/tinyxml_2_4_0.tar.gz下载tinyxml,可以根据自己的需要,选择不同的版本。
将tinyxml_2_4_0.tar.gz上传到主机,然后解压执行如下命令:
tar -xzvf tinyxml_2_4_0.tar.gz
成功之后,会在当前目录出现一个tinyxml目录,进入该目录cd tinyxml,然后进行编译,顺序执行如下命令:
cd tinyxml
make
在屏幕上会打印如下输出:
- [dev@localhost tinyxml]$ make
- g++ -c -Wall -Wno-format -g -DDEBUG tinyxml.cpp -o tinyxml.o
- g++ -c -Wall -Wno-format -g -DDEBUG tinyxmlparser.cpp -o tinyxmlparser.o
- g++ -c -Wall -Wno-format -g -DDEBUG xmltest.cpp -o xmltest.o
- g++ -c -Wall -Wno-format -g -DDEBUG tinyxmlerror.cpp -o tinyxmlerror.o
- g++ -c -Wall -Wno-format -g -DDEBUG tinystr.cpp -o tinystr.o
- tinystr.cpp:38: warning: aggregate has a partly bracketed initializer
- g++ -o xmltest -g tinyxml.o tinyxmlparser.o xmltest.o tinyxmlerror.o tinystr.o
没有出现错误,表示编译完成,这时可以执行tinyxml自带的测试程序xmltest。
我直行xmltest之后,打印出一堆乱码,后来就没有管。自己写程序测试了。
为了使用tinyxml开发,使用方便,做了一些配置。
添加环境变量TINYXML_ROOT,编辑.bash_profile,添加如下内容:
- #############################################
- #### for tinyxml
- #############################################
- export TINYXML_ROOT=$HOME/tinyxml
把tinyxml包编译打包成一个连接库,方便开发,这就要修改tinyxml目录下的Makefile。
在tinyxml目录下重新执行make,会看到多执行了一行命令:
ar -r libtinyxml.so tinyxml.o tinyxmlparser.o tinyxmlerror.o tinystr.o
生成了一个包libtinyxml.so,有了这个包,使用tinyxml开发的时候,在连接命令中加入这个包的连接,就可以正确地生成目标程序。
2、使用tinyxml库
TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。
DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。
首先从网上下载TinyXML的库,文件夹的名字是TinyXpath,在工程里做如下配置:
在附加包含路径里添加:你的tinyxpath路径/tinyxpath/include
在附加库路径里添加:你的tinyxpath路径/tinyxpath/lib
在对象/库路径里添加:tinyxpathd.lib,如果使用release版本,则是tinyxpath.lib。
另外,由于我开发的项目是多线程的,所以设置了多线程的环境,因此使用TinyXML没有出现问题。本人将TinyXML写在一个单独的C++工程进行测试,发现如果不设置多线程的环境,会出现链接错误。我觉得原因可能是TinyXML使用了多线程环境,因此需要设置
多线程的环境。在工程/设置下的C/C++选项卡中,选择Code Generation,在Use run-time library中选择Debug MultiThreaed DLL即可。
本例的XML文件Students.xml如下:
id="iframe_0.238658958169796" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.238658958169796',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 11px; height: 16px;">
"计算机软件班">
id="iframe_0.4314071120123457" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.4314071120123457',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.5366316319000168" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5366316319000168',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> "张三" studentNo=
"13031001" sex=
"男" age=
"22">
id="iframe_0.21304391208801743" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.21304391208801743',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> 88208888
id="iframe_0.9251619861410729" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9251619861410729',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> 西安市太白南路二号
id="iframe_0.6021517642700922" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6021517642700922',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.6341668334925294" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6341668334925294',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> "李四" studentNo=
"13031002" sex=
"男" age=
"20">
id="iframe_0.802585094038172" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.802585094038172',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> 88206666
id="iframe_0.44781580335367255" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.44781580335367255',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> 西安市光华路
id="iframe_0.11384552384977575" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.11384552384977575',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.05060587505102476" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.05060587505102476',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
id="iframe_0.916137101333931" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.916137101333931',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
程序代码XmlParseExample.cpp如下所示:
id="iframe_0.5045835055573049" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5045835055573049',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">#include
id="iframe_0.12717277723937692" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.12717277723937692',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">#include <
string>
id="iframe_0.7911499984094643" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7911499984094643',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">#include
id="iframe_0.6797304277618614" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6797304277618614',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
using std::
string;
id="iframe_0.4726179124668808" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.4726179124668808',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
int main()
id="iframe_0.8307900466320224" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8307900466320224',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">{
id="iframe_0.1861888369931608" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.1861888369931608',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> TiXmlDocument* myDocument =
new TiXmlDocument();
id="iframe_0.8070116582035924" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8070116582035924',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> myDocument->LoadFile(
"Students.xml");
id="iframe_0.7752662136819903" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7752662136819903',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> TiXmlElement* rootElement = myDocument->RootElement();
//Class
id="iframe_0.12817875489448105" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.12817875489448105',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> TiXmlElement* studentsElement = rootElement->FirstChildElement();
//Students
id="iframe_0.18624075806944407" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.18624075806944407',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> TiXmlElement* studentElement = studentsElement->FirstChildElement();
//Students
id="iframe_0.2740520439978009" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.2740520439978009',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
while ( studentElement ) {
id="iframe_0.07987582854164255" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.07987582854164255',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> TiXmlAttribute* attributeOfStudent = studentElement->FirstAttribute();
//获得student的name属性
id="iframe_0.851699008283888" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.851699008283888',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
while ( attributeOfStudent ) {
id="iframe_0.00541869841883047" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.00541869841883047',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> std::cout << attributeOfStudent->Name() <<
" : " << attributeOfStudent->Value() << std::endl;
id="iframe_0.5832653685379883" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5832653685379883',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> attributeOfStudent = attributeOfStudent->Next();
id="iframe_0.061893995195918095" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.061893995195918095',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> }
id="iframe_0.00529127955311548" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.00529127955311548',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> TiXmlElement* phoneElement = studentElement->FirstChildElement();
//获得student的phone元素
id="iframe_0.36240679789353725" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.36240679789353725',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> std::cout <<
"phone" <<
" : " << phoneElement->GetText() << std::endl;
id="iframe_0.9596108977233886" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9596108977233886',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> TiXmlElement* addressElement = phoneElement->NextSiblingElement();
id="iframe_0.8693341939749397" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8693341939749397',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> std::cout <<
"address" <<
" : " << phoneElement->GetText() << std::endl;
id="iframe_0.09740815378216272" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.09740815378216272',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> studentElement = studentElement->NextSiblingElement();
id="iframe_0.23872697428271672" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.23872697428271672',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> }
id="iframe_0.8990366175957798" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8990366175957798',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
return 0;
id="iframe_0.03892032775494392" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.03892032775494392',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">}
程序运行结果如下:
name : 张三
studentNo : 13031001
sex : 男
age : 22
phone : 88208888
address : 88208888
name : 李四
studentNo : 13031002
sex : 男
age : 20
phone : 88206666
address : 88206666
本例中使用的是对xml文件进行解析,很容易掌握,但是很多开发人员不知道如何对xml 字符流(非xml文件)进行解析,我看了TinyXML提供的源代码,里面可以使用如下方法对xml流解析。对应于上例,代码如下:
id="iframe_0.735328356988513" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.735328356988513',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;">
string xmlString =
id="iframe_0.023006223477946897" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.023006223477946897',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> "
\
id="iframe_0.5864910718611323" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5864910718611323',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> \
id="iframe_0.3596549137063396" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.3596549137063396',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> \
id="iframe_0.9128635095195798" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9128635095195798',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> 88208888\
id="iframe_0.2400037559929149" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.2400037559929149',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> 西安市太白南路二号\
id="iframe_0.8826393390392213" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8826393390392213',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> \
id="iframe_0.08245379558500332" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.08245379558500332',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> \
id="iframe_0.3244030647776621" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.3244030647776621',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> 88206666\
id="iframe_0.3931108914639201" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.3931108914639201',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> 西安市光华路\
id="iframe_0.9682242754411596" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9682242754411596',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> \
id="iframe_0.25672920789727693" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.25672920789727693',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> \
id="iframe_0.9453442512246908" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9453442512246908',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> ";
id="iframe_0.9535319498883794" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9535319498883794',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> TiXmlDocument* myDocument =
new TiXmlDocument();
id="iframe_0.7544976750332633" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://panpan.blog.51cto.com/images/editer/InBlock.gif?_=4995618%22%20style=%22border:none;max-width:1017px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7544976750332633',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 0px; height: 0px;"> myDocument->Parse(xmlString.c_str());
使用Parse函数就可以解析XML字符流了,这是很多开发者不太熟悉的情况。
如果开发者开发特定应用,就可以使用上述类似方法,可能不需要完全处理每一个属性,比如可以对属性名进行判断,只处理自己需要的属性,或者自己需要的xml元素。还可以使用TinyXML的方法创建xml元素和xml属性,或者设置xml元素和属性对应的值,等等,如果读者想要类似的例子,可以留言写出。
下面介绍TinyXML的一些类。在TinyXML中,根据XML的各种元素来定义了一些类:
TiXmlBase:整个TinyXML模型的基类。
TiXmlAttribute:对应于XML中的元素的属性。
TiXmlNode:对应于DOM结构中的节点。
TiXmlComment:对应于XML中的注释
TiXmlDeclaration:对应于XML中的申明部分,<?versiong="1.0" ?>。
TiXmlDocument:对应于XML的整个文档。
TiXmlElement:对应于XML的元素。
TiXmlText:对应于XML的文字部分
TiXmlUnknown:对应于XML的未知部分。
TiXmlHandler:定义了针对XML的一些操作。
3、linux中动态库的使用
编译生产libcac.so文件如下:
gcc -shared -fPIC caculate.c -o libcac.so
编译生产可执行文件main如下:gcc main.c -o main -L ./ -lcac (其中-L指明动态链接库的路径,-l后是链接库的名称,省略lib)