C++它tinyXML使用
2015-07-02 14:27
716 查看
tinyXML一个非常好的操作C++图书馆,文件不大,但方法非常丰富。和apache的Dom4j能够披靡啊!
习惯了使用java类库的我看到这么丰富的c++类库,非常高兴!它使用非常easy。仅仅须要拷贝几个文件到你的project中,没有STL也能够编译执行。
以下我从这几个方面谈谈对tinyXML类库的使用以及理解。
首先在sourceforge上下载tinyXML类库,地址:http://sourceforge.net/projects/tinyxml/
然后解压缩tinyXML后,将这六个文件加入到你的c++project中,各自是tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。在须要操作xml文件的地方,使用例如以下代码,就能够引入tinyXML类库。
C++代码
#include<tinyxml>
或者
C++代码
#include "tinyxml.h"
以下我用个简单的样例说明怎样使用tinyXML操作xml文件。在讲样例之前我先说说tinyXML中主要类和xml文档之间的相应关系。以下是tinyXML中主要class的类图,反应各个类之间的静态关系。
引用来自tinyXML文档
TiXmlBase是全部类的基类。TiXmlNode、TiXmlAttribute两个类都继承来自TiXmlBase类,当中TiXmlNode类指的是全部被<...>...<.../>包含的内容,而xml中的节点又详细分为下面几方面内容,各自是声明、凝视、节点以及节点间的文本,因此在TiXmlNode的基础上又衍生出这几个类TiXmlComment、TiXmlDeclaration、TiXmlDocument、TiXmlElement、TiXmlText、TiXmlUnknown。分别用来指明详细是xml中的哪一部分。TiXmlAttribute类不同于TiXmlNode,它指的是在尖括号中面的内容,像<...
***=...>,当中***就是一个属性。
这块我详细用一个xml文档说明一下,内容例如以下:
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<phonebook>
<!--one item behalfs one contacted person.-->
<item>
<name>miaomaio</name>
<addr>Shaanxi Xi'an</addr>
<tel>13759911917</tel>
<email>miaomiao@home.com</email>
</item>
<item>
<name>gougou</name>
<addr>Liaoning Shenyang</addr>
<tel>15840330481</tel>
<email>gougou@home.com</email>
</item>
<!--more contacted persons.-->
</phonebook>
像TiXmlDeclaration指的就是<?
xml version="1.0" encoding="UTF-8"?
>。
像TiXmlComment指的就是<!--one item behalfs one contacted person.-->、 <!--more contacted persons.-->,
像TiXmlDocument指的就是整个xml文档,
像TiXmlElement指的就是<phonebook>、<item>、<name>、<addr>等等这些节点,
像TiXmlText指的就是‘gougou’、‘15840330481’这些夹在<item>与</item>、<name>与</name>、<addr>与</addr>之间的文本文字,
像TiXmlAttribute指的就是<?xml version="1.0" encoding="UTF-8"?>节点中version、encoding。
除此之外就是TiXmlUnknown。
以下是我自己写的一段读xml文件的c++代码,以及再往xml写入一个item的源码,当中phonebookdata.xml中的内容就是上面xml,仅供參考。
C++代码
//______________________________________________________________________
// Read information from xml file.
// define xml file path, as follow , we use relative path,
// but you can use absolute path also.
const char* filepath = "phonebookdata.xml";
TiXmlDocument doc(filepath);
bool loadOkay = doc.LoadFile();
// faile to load 'phonebookdata.xml'.
if (!loadOkay) {
printf( "Could not load test file %s. Error='%s'. Exiting.\n", filepath,doc.ErrorDesc() );
exit( 1 );
}
// get dom root of 'phonebookdata.xml', here root should be 'phonebook'.
TiXmlElement* root = doc.RootElement();
printf("_______________________________________\n\n");
printf(" contacted person information \n\n");
// trace every items below root.
for( TiXmlNode* item = root->FirstChild( "item" );
item;
item = item->NextSibling( "item" ) ) {
printf("_______________________________________\n");
// read name.
TiXmlNode* child = item->FirstChild();
const char* name = child->ToElement()->GetText();
if (name) {
printf("name:%s\n",name);
} else {
printf("name:\n");
}
// read address.
child = item->IterateChildren(child);
const char* addr = child->ToElement()->GetText();
if (addr) {
printf("addr:%s\n",addr);
} else {
printf("addr:\n");
}
// read telephone no.
child = item->IterateChildren(child);
const char* tel = child->ToElement()->GetText();
if (tel) {
printf("tel:%s\n",tel);
} else {
printf("tel:\n");
}
// read e-mail.
child = item->IterateChildren(child);
const char* email = child->ToElement()->GetText();
if(email) {
printf("email:%s\n",email);
} else {
printf("email:\n");
}
printf("\n");
}
//______________________________________________________________________
//______________________________________________________________________
// Add information to xml file and save it.
TiXmlElement* writeRoot = doc.RootElement();
TiXmlNode* newNode = new TiXmlElement("item");
const TiXmlNode* name4NewNode = new TiXmlElement("name");
newNode->InsertEndChild(*name4NewNode)->InsertEndChild(TiXmlText("pipi"));
const TiXmlNode* addr4NewNode = new TiXmlElement("addr");
newNode->InsertEndChild(*addr4NewNode)->InsertEndChild(TiXmlText("Shaanxi Xianyang"));
const TiXmlNode* tel4NewNode = new TiXmlElement("tel");
newNode->InsertEndChild(*tel4NewNode)->InsertEndChild(TiXmlText("02937310627"));
const TiXmlNode* email4NewNode = new TiXmlElement("email");
newNode->InsertEndChild(*email4NewNode)->InsertEndChild(TiXmlText("pipi@home.com"));
writeRoot->InsertEndChild(*newNode);
doc.SaveFile();
//______________________________________________________________________
具体用法可以参考tinyxml由文件陪同。
习惯了使用java类库的我看到这么丰富的c++类库,非常高兴!它使用非常easy。仅仅须要拷贝几个文件到你的project中,没有STL也能够编译执行。
以下我从这几个方面谈谈对tinyXML类库的使用以及理解。
首先在sourceforge上下载tinyXML类库,地址:http://sourceforge.net/projects/tinyxml/
然后解压缩tinyXML后,将这六个文件加入到你的c++project中,各自是tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。在须要操作xml文件的地方,使用例如以下代码,就能够引入tinyXML类库。
C++代码
#include<tinyxml>
或者
C++代码
#include "tinyxml.h"
以下我用个简单的样例说明怎样使用tinyXML操作xml文件。在讲样例之前我先说说tinyXML中主要类和xml文档之间的相应关系。以下是tinyXML中主要class的类图,反应各个类之间的静态关系。
引用来自tinyXML文档
TiXmlBase是全部类的基类。TiXmlNode、TiXmlAttribute两个类都继承来自TiXmlBase类,当中TiXmlNode类指的是全部被<...>...<.../>包含的内容,而xml中的节点又详细分为下面几方面内容,各自是声明、凝视、节点以及节点间的文本,因此在TiXmlNode的基础上又衍生出这几个类TiXmlComment、TiXmlDeclaration、TiXmlDocument、TiXmlElement、TiXmlText、TiXmlUnknown。分别用来指明详细是xml中的哪一部分。TiXmlAttribute类不同于TiXmlNode,它指的是在尖括号中面的内容,像<...
***=...>,当中***就是一个属性。
这块我详细用一个xml文档说明一下,内容例如以下:
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<phonebook>
<!--one item behalfs one contacted person.-->
<item>
<name>miaomaio</name>
<addr>Shaanxi Xi'an</addr>
<tel>13759911917</tel>
<email>miaomiao@home.com</email>
</item>
<item>
<name>gougou</name>
<addr>Liaoning Shenyang</addr>
<tel>15840330481</tel>
<email>gougou@home.com</email>
</item>
<!--more contacted persons.-->
</phonebook>
像TiXmlDeclaration指的就是<?
xml version="1.0" encoding="UTF-8"?
>。
像TiXmlComment指的就是<!--one item behalfs one contacted person.-->、 <!--more contacted persons.-->,
像TiXmlDocument指的就是整个xml文档,
像TiXmlElement指的就是<phonebook>、<item>、<name>、<addr>等等这些节点,
像TiXmlText指的就是‘gougou’、‘15840330481’这些夹在<item>与</item>、<name>与</name>、<addr>与</addr>之间的文本文字,
像TiXmlAttribute指的就是<?xml version="1.0" encoding="UTF-8"?>节点中version、encoding。
除此之外就是TiXmlUnknown。
以下是我自己写的一段读xml文件的c++代码,以及再往xml写入一个item的源码,当中phonebookdata.xml中的内容就是上面xml,仅供參考。
C++代码
//______________________________________________________________________
// Read information from xml file.
// define xml file path, as follow , we use relative path,
// but you can use absolute path also.
const char* filepath = "phonebookdata.xml";
TiXmlDocument doc(filepath);
bool loadOkay = doc.LoadFile();
// faile to load 'phonebookdata.xml'.
if (!loadOkay) {
printf( "Could not load test file %s. Error='%s'. Exiting.\n", filepath,doc.ErrorDesc() );
exit( 1 );
}
// get dom root of 'phonebookdata.xml', here root should be 'phonebook'.
TiXmlElement* root = doc.RootElement();
printf("_______________________________________\n\n");
printf(" contacted person information \n\n");
// trace every items below root.
for( TiXmlNode* item = root->FirstChild( "item" );
item;
item = item->NextSibling( "item" ) ) {
printf("_______________________________________\n");
// read name.
TiXmlNode* child = item->FirstChild();
const char* name = child->ToElement()->GetText();
if (name) {
printf("name:%s\n",name);
} else {
printf("name:\n");
}
// read address.
child = item->IterateChildren(child);
const char* addr = child->ToElement()->GetText();
if (addr) {
printf("addr:%s\n",addr);
} else {
printf("addr:\n");
}
// read telephone no.
child = item->IterateChildren(child);
const char* tel = child->ToElement()->GetText();
if (tel) {
printf("tel:%s\n",tel);
} else {
printf("tel:\n");
}
// read e-mail.
child = item->IterateChildren(child);
const char* email = child->ToElement()->GetText();
if(email) {
printf("email:%s\n",email);
} else {
printf("email:\n");
}
printf("\n");
}
//______________________________________________________________________
//______________________________________________________________________
// Add information to xml file and save it.
TiXmlElement* writeRoot = doc.RootElement();
TiXmlNode* newNode = new TiXmlElement("item");
const TiXmlNode* name4NewNode = new TiXmlElement("name");
newNode->InsertEndChild(*name4NewNode)->InsertEndChild(TiXmlText("pipi"));
const TiXmlNode* addr4NewNode = new TiXmlElement("addr");
newNode->InsertEndChild(*addr4NewNode)->InsertEndChild(TiXmlText("Shaanxi Xianyang"));
const TiXmlNode* tel4NewNode = new TiXmlElement("tel");
newNode->InsertEndChild(*tel4NewNode)->InsertEndChild(TiXmlText("02937310627"));
const TiXmlNode* email4NewNode = new TiXmlElement("email");
newNode->InsertEndChild(*email4NewNode)->InsertEndChild(TiXmlText("pipi@home.com"));
writeRoot->InsertEndChild(*newNode);
doc.SaveFile();
//______________________________________________________________________
具体用法可以参考tinyxml由文件陪同。
相关文章推荐
- [原创]C语言利用pcre正则表达式库
- LeetCode-House Robber II-解题报告
- C++11中的std::function
- 最小二乘法椭圆拟合
- LeetCode-Shortest Palindrome-解题报告
- LeetCode-Kth Smallest Element in a BST-解题报告
- C++—函数探幽
- C语言中main函数的参数
- Java调用C语言
- VC++ List Control 的具体用法实例
- 关于C++派生类中构造函数调用顺序的问题
- LeetCode-Best Time to Buy and Sell Stock IV -解题报告
- 第十七周oj刷题——Problem B: 分数类的四则运算【C++】
- C++服务编程
- LeetCode-Best Time to Buy and Sell Stock III -解题报告
- C语言中const的用法
- (7)风色从零单排《C++ Primer》 string
- 给出年、月、日,计算该日是该年的第几天。
- C++ 排序函数 sort(),qsort()的用法
- C++ STL中Map的按Key排序和按Value排序