使用TinyXML解析XML文件
2017-12-27 00:00
387 查看
1.介绍读取和设置xml配置文件是最常用的操作,TinyXML是一个开源的解析XML的C++解析库,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。 下载TinyXML的网址:http://www.grinninglizard.com/tinyxml/使用TinyXML只需要将其中的6个文件拷贝到项目中就可以直接使用了,这六个文件是:tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。 2.读取XML文件 如读取文件a.xml:
读取代码如下:#include "tinyxml.h"#include <iostream>#include <string>usingnamespace std;enumSuccessEnum {FAILURE, SUCCESS};SuccessEnum loadXML(){TiXmlDocument doc;if(!doc.LoadFile("a.xml")){ cerr<< doc.ErrorDesc() << endl; return FAILURE;}TiXmlElement* root = doc.FirstChildElement();if(root== NULL){ cerr<< "Failed to loadfile: No root element."<< endl; doc.Clear(); return FAILURE; } for(TiXmlElement*elem = root->FirstChildElement(); elem != NULL; elem =elem->NextSiblingElement()){ string elemName = elem->Value(); constchar* attr; attr= elem->Attribute("priority"); if(strcmp(attr,"1")==0) { TiXmlElement*e1 = elem->FirstChildElement("bold"); TiXmlNode* e2=e1->FirstChild(); cout<<"priority=1\t"<<e2->ToText()->Value()<<endl; } elseif(strcmp(attr,"2")==0) { TiXmlNode* e1 = elem->FirstChild(); cout<<"priority=2\t"<<e1->ToText()->Value()<<endl; }}doc.Clear();return SUCCESS;} int main(int argc, char* argv[]){if(loadXML()== FAILURE) return1;return0;}
[b]3.生成XML文件
[/b] 如生成文件b.xml如下所示:
生成上面b.xmlL文件代码如下:
#include "tinyxml.h"
#include <iostream>
#include <string>
using namespace std;
enum SuccessEnum {FAILURE, SUCCESS};
SuccessEnum saveXML()
{
TiXmlDocument doc;
TiXmlElement*root = new TiXmlElement("root");
doc.LinkEndChild(root);
TiXmlElement* element1 = new TiXmlElement("Element1");
root->LinkEndChild(element1);
element1->SetAttribute("attribute1", "somevalue");
TiXmlElement* element2 = new TiXmlElement("Element2"); ///元素
root->LinkEndChild(element2);
element2->SetAttribute("attribute2","2");
element2->SetAttribute("attribute3","3");
TiXmlElement* element3 = new TiXmlElement("Element3");
element2->LinkEndChild(element3);element3->SetAttribute("attribute4", "4");
TiXmlText*text = new TiXmlText("Some text."); ///文本
element2->LinkEndChild(text);
bool success = doc.SaveFile("b.xml");
doc.Clear();
if(success)
return SUCCESS;
else
return FAILURE;
}
int main(int argc, char* argv[])
{
if(saveXML() == FAILURE)
return 1;
return 0;
}
4.重要函数或类型的说明(1)FirstChildElement(const char* value=0):获取第一个值为value的子节点,value默认值为空,则返回第一个子节点。(2)NextSiblingElement( const char* _value=0 ) :获得下一个(兄弟)节点。(3)LinkEndChild(XMLHandle *handle):添加一个子节点。元素或者文本
读取代码如下:#include "tinyxml.h"#include <iostream>#include <string>usingnamespace std;enumSuccessEnum {FAILURE, SUCCESS};SuccessEnum loadXML(){TiXmlDocument doc;if(!doc.LoadFile("a.xml")){ cerr<< doc.ErrorDesc() << endl; return FAILURE;}TiXmlElement* root = doc.FirstChildElement();if(root== NULL){ cerr<< "Failed to loadfile: No root element."<< endl; doc.Clear(); return FAILURE; } for(TiXmlElement*elem = root->FirstChildElement(); elem != NULL; elem =elem->NextSiblingElement()){ string elemName = elem->Value(); constchar* attr; attr= elem->Attribute("priority"); if(strcmp(attr,"1")==0) { TiXmlElement*e1 = elem->FirstChildElement("bold"); TiXmlNode* e2=e1->FirstChild(); cout<<"priority=1\t"<<e2->ToText()->Value()<<endl; } elseif(strcmp(attr,"2")==0) { TiXmlNode* e1 = elem->FirstChild(); cout<<"priority=2\t"<<e1->ToText()->Value()<<endl; }}doc.Clear();return SUCCESS;} int main(int argc, char* argv[]){if(loadXML()== FAILURE) return1;return0;}
[b]3.生成XML文件
[/b] 如生成文件b.xml如下所示:
生成上面b.xmlL文件代码如下:
#include "tinyxml.h"
#include <iostream>
#include <string>
using namespace std;
enum SuccessEnum {FAILURE, SUCCESS};
SuccessEnum saveXML()
{
TiXmlDocument doc;
TiXmlElement*root = new TiXmlElement("root");
doc.LinkEndChild(root);
TiXmlElement* element1 = new TiXmlElement("Element1");
root->LinkEndChild(element1);
element1->SetAttribute("attribute1", "somevalue");
TiXmlElement* element2 = new TiXmlElement("Element2"); ///元素
root->LinkEndChild(element2);
element2->SetAttribute("attribute2","2");
element2->SetAttribute("attribute3","3");
TiXmlElement* element3 = new TiXmlElement("Element3");
element2->LinkEndChild(element3);element3->SetAttribute("attribute4", "4");
TiXmlText*text = new TiXmlText("Some text."); ///文本
element2->LinkEndChild(text);
bool success = doc.SaveFile("b.xml");
doc.Clear();
if(success)
return SUCCESS;
else
return FAILURE;
}
int main(int argc, char* argv[])
{
if(saveXML() == FAILURE)
return 1;
return 0;
}
4.重要函数或类型的说明(1)FirstChildElement(const char* value=0):获取第一个值为value的子节点,value默认值为空,则返回第一个子节点。(2)NextSiblingElement( const char* _value=0 ) :获得下一个(兄弟)节点。(3)LinkEndChild(XMLHandle *handle):添加一个子节点。元素或者文本
相关文章推荐
- C++使用TinyXML生成和解析xml文件
- 使用tinyxml创建及解析XML文件
- 浅谈如何使用tinyxml解析xml文件
- C++使用tinyxml创建和解析xml文件(本文用于cocos2dx)
- cocos2dx使用tinyxml创建和解析xml文件
- Linux下C++使用TinyXML解析XML文件
- 使用TinyXML进行xml文件解析,生成xml文件
- C++ 使用 TinyXml 解析 XML 文件
- 使用TinyXml 解析XML文件
- C++ 使用TinyXML解析XML文件
- [Cocos2d-x] 使用TinyXml 解析XML文件
- Cocos2d-x 3.0 使用TinyXml 解析XML文件
- Android使用Pull解析器解析XML文件
- 使用JDOM创建和解析XML文件
- perl 解析xml ,json文件及curl工具的使用
- 使用Dom生成xml文件和解析xml文件
- 使用dom4j来解析,生成xml文件
- 使用Struts Digester来解析XML文件
- android 使用 pull 解析xml文件
- 使用Struts Digester来解析XML文件