大家好,又见面了,我是你们的朋友全栈君。
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
在屏幕上会打印如下输出:
没有出现错误,表示编译完成,这时可以执行tinyxml自带的测试程序xmltest。
我直行xmltest之后,打印出一堆乱码,后来就没有管。自己写程序测试了。
为了使用tinyxml开发,使用方便,做了一些配置。
添加环境变量TINYXML_ROOT,编辑.bash_profile,添加如下内容:
把tinyxml包编译打包成一个连接库,方便开发,这就要修改tinyxml目录下的Makefile。
在tinyxml目录下重新执行make,会看到多执行了一行命令:
ar -r libtinyxml.so tinyxml.o tinyxmlparser.o tinyxmlerror.o tinystr.o
生成了一个包libtinyxml.so,有了这个包,使用tinyxml开发的时候,在连接命令中加入这个包的连接,就可以正确地生成目标程序。
2、使用tinyxml库
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.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;”> <Students>
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;”> <student name=
“张三” 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;”> <phone>88208888</phone>
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;”> <address>西安市太白南路二号</address>
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;”> </student>
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;”> <student name=
“李四” 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;”> <phone>88206666</phone>
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;”> <address>西安市光华路</address>
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;”> </student>
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;”> </Students>
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;”></Class>
程序代码XmlParseExample.cpp如下所示:
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 <tinyxml.h>
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流解析。对应于上例,代码如下:
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;”> “<Class name=\”计算机软件班\”>\
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;”> <Students>\
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;”> <student name=\”张三\” studentNo=\”13031001\” sex=\”男\” age=\”22\”>\
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;”> <phone>88208888</phone>\
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;”> <address>西安市太白南路二号</address>\
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;”> </student>\
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;”> <student name=\”李四\” studentNo=\”13031002\” sex=\”男\” age=\”20\”>\
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;”> <phone>88206666</phone>\
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;”> <address>西安市光华路</address>\
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;”> </student>\
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;”> </Students>\
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;”> </Class>”;
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字符流了,这是很多开发者不太熟悉的情况。
下面介绍TinyXML的一些类。在TinyXML中,根据XML的各种元素来定义了一些类:
TiXmlBase:整个TinyXML模型的基类。
TiXmlHandler:定义了针对XML的一些操作。
3、linux中动态库的使用
gcc -shared -fPIC caculate.c -o libcac.so
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/134043.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...