标签:
借鉴于:http://www.cppblog.com/elva/archive/2008/04/24/47907.html
例如:
1 <?xml version="1.0" standalone=no> 2 <!– Our to do list data –> 3 <ToDo> 4 <Item priority="1"> Go to the <bold>Toy store!</bold></Item> 5 <Item priority="2"> Do bills</Item> 6 </ToDo>
想要健壮地读取一个XML文档,检查方法调用后的返回值是否为null是很重要的。一种安全的检错实现可能会产生像这样的代码:
1 TiXmlElement* root = document.FirstChildElement( "Document" ); 2 if ( root ) 3 { 4 TiXmlElement* element = root->FirstChildElement( "Element" ); 5 if ( element ) 6 { 7 TiXmlElement* child = element->FirstChildElement( "Child" ); 8 if ( child ) 9 { 10 TiXmlElement* child2 = child->NextSiblingElement( "Child" ); 11 if ( child2 ) 12 { 13 // Finally do something useful.
用句柄(TiXmlHandle)的话就不会这么冗长了,使用TiXmlHandle类,前面的代码就会变成这样:
1 TiXmlHandle docHandle( &document ); 2 TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).ToElement(); 3 if ( child2 ) 4 { 5 // do something useful
一、读取XML,设置节点文本
如下XML片段:
1 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 2 <ZXML> 3 <ZAPP> 4 <VBS_RUNTIME_PARAMS> 5 <BROADCAST_VERSION info="版本">8</BROADCAST_VERSION> 6 <Broadcast> 7 <FileCount info="资源文件个数">69</FileCount> 8 <SOURCE_1> 9 <ID info="图片编号">1</ID> 10 <Version info="图片版本">1</Version> 11 <Path info="图片路径">/mnt/share/1.bmp</Path> 12 <FileMode info="文件处理模式">0</FileMode> 13 </SOURCE_1> 14 <SOURCE_2> 15 <Path info="图片路径">/mnt/share/2.bmp</Path> 16 <ID info="图片编号">2</ID> 17 <Version info="图片版本">1</Version> 18 <FileMode info="文件处理模式">0</FileMode> 19 </SOURCE_2> 20 . 21 </Broadcast> 22 </VBS_RUNTIME_PARAMS> 23 </ZAPP> 24 </ZXML>
要设置BROADCAST_VERSION节点的值 8为其他值,可参考如下代码(将值加1):
用ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis )方法替换
1 TiXmlDocument doc("zapp.conf"); 2 doc.LoadFile(); 3 TiXmlHandle docHandle( &doc ); 4 TiXmlElement* Broadcast_ver = docHandle.FirstChild("ZXML").FirstChild("ZAPP").FirstChild("VBS_RUNTIME_PARAMS").FirstChildElement("BROADCAST_VERSION").ToElement(); 5 TiXmlNode * oldnode = Broadcast_ver->FirstChild(); 6 const char *ver = Broadcast_ver->GetText(); 7 int oldVer = atoi(ver); 8 CString newVer; 9 newVer.Format("%d",oldVer+1); 10 TiXmlText newText(newVer); 11 Broadcast_ver->ReplaceChild(oldnode,newText); 12 AfxMessageBox(Broadcast_ver->GetText());//输出值 13 doc.SaveFile();
二,删除节点,属性值
RemoveChild( TiXmlNode* removeThis )方法删除父节点的子节点,
RemoveAttribute( const char * name )方法删除属性值.
例如删除BROADCAST_VERSION节点
1 TiXmlHandle docHandle( &doc ); 2 TiXmlElement* Broadcast_ver = docHandle.FirstChild("ZXML").FirstChild("ZAPP").FirstChild("VBS_RUNTIME_PARAMS").ToElement(); 3 4 TiXmlNode * node = Broadcast_ver->FirstChild("BROADCAST_VERSION"); 5 6 Broadcast_ver->RemoveChild(node);
也可以删除整个SOURCE_1节点:
1 TiXmlHandle docHandle( &doc ); 2 TiXmlElement* Broadcast = docHandle.FirstChild("ZXML").FirstChild("ZAPP").FirstChild("VBS_RUNTIME_PARAMS").FirstChild("Broadcast").ToElement(); 3 4 TiXmlNode * node = Broadcast->FirstChild("SOURCE_1"); 5 6 Broadcast->RemoveChild(node);
删除BROADCAST_VERSION的info属性:
1 TiXmlHandle docHandle( &doc ); 2 TiXmlElement* Broadcast_ver = docHandle.FirstChild("ZXML").FirstChild("ZAPP").FirstChild("VBS_RUNTIME_PARAMS").FirstChildElement("BROADCAST_VERSION").ToElement(); 3 4 Broadcast_ver->RemoveAttribute("info"); //删除info
可以借助NextSiblingElement()方法实现递归删除.
三,添加节点,属性值
例如在SOURCE_3下添加BROADCAST_PID节点:
1 TiXmlHandle docHandle( &doc ); 2 TiXmlElement* Broadcast = docHandle.FirstChild("ZXML").FirstChild("ZAPP").FirstChild("VBS_RUNTIME_PARAMS").FirstChild("Broadcast").ToElement(); 3 TiXmlElement* Broadcast_Pid = new TiXmlElement("BROADCAST_PID"); 4 TiXmlText *text =new TiXmlText("7215"); 5 Broadcast_Pid->SetAttribute("info","the pid"); 6 Broadcast_Pid->LinkEndChild(text); 7 Broadcast->LinkEndChild(Broadcast_Pid);
将在SOURCE_3后添加新的节点:
1 <BROADCAST_PID info="the pid">7215</BROADCAST_PID>
四,最后说一下中文乱码的问题
乱码是由于GB2312与UTF8之间转换不当造成的,tinyxml在处理UTF8本身没有问题,当你打开一个UTF8的文档,可以在加载的时候指定UTF8的方式,或者文档声明处指明的编码格式,tinyxml会按照相应的编码格式加载,但很多时候当我们输出或写入中文字段时会出现乱码,无论在内存,还是打印出来的内容.这是因为我们的软件通常是GB2312编码,而读取或写入的内容是UTF8,自然就会出错.可以借助网上的两个函数来实现转换(原作者不详):
1 void ConvertUtf8ToGBK(CString& strUtf8) 2 { 3 int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0); 4 unsigned short * wszGBK = new unsigned short[len+1]; 5 memset(wszGBK, 0, len * 2 + 2); 6 MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len); 7 8 len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); 9 char *szGBK=new char[len + 1]; 10 memset(szGBK, 0, len + 1); 11 WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL); 12 13 strUtf8 = szGBK; 14 delete[] szGBK; 15 delete[] wszGBK; 16 } 17 18 19 void ConvertGBKToUtf8(CString& strGBK) 20 { 21 int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0); 22 unsigned short * wszUtf8 = new unsigned short[len+1]; 23 memset(wszUtf8, 0, len * 2 + 2); 24 MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len); 25 26 len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL); 27 char *szUtf8=new char[len + 1]; 28 memset(szUtf8, 0, len + 1); 29 WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL); 30 31 strGBK = szUtf8; 32 delete[] szUtf8; 33 delete[] wszUtf8; 34 }
当然,你也可以用MultiByteToWideChar,WideCharToMultiByte函数自己实现转换.以上是简单应用的几个举例,理解他们,相信你已经能写出满足自己需要的代码了.
标签:
原文地址:http://www.cnblogs.com/shmilxu/p/4831351.html