tinyxml使用
Posted xhBruce
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tinyxml使用相关的知识,希望对你有一定的参考价值。
tinyxml使用
http://www.grinninglizard.com/tinyxml/
https://sourceforge.net/projects/tinyxml/
GitHub:leethomason/tinyxml2
在线文档:TinyXml Documentation
tinyxml 下载
tinyxml_2_6_2.zip
文档:tinyxml_2_6_2.zip/tinyxml/docs/index.html
导入.cpp和.h文件
- 如Dev C++新建项目tinyxml
- 导入.cpp和.h文件
- 运行xmltest.cpp
- 添加utf8test.xml,对照查看用法
<?xml version="1.0" encoding="UTF-8"?>
<document>
<English name="name" value="value">The world has many languages</English>
<Russian name="название(имя)" value="ценность">Мир имеет много языков</Russian>
<Spanish name="el nombre" value="el valor">el mundo tiene muchos idiomas</Spanish>
<SimplifiedChinese name="名字" value="价值">世界有很多语言</SimplifiedChinese>
<Русский название="name" ценность="value"><имеет></Русский>
<汉语 名字="name" 价值="value">世界有很多语言</汉语>
<Heavy>"Mëtæl!"</Heavy>
<ä>Umlaut Element</ä>
</document>
xml读取
获取某个标签
doc.Print()
输出整个xml- xml中"document"下"English"标签
- TiXmlAttribute 获取并输出"English"标签属性
- element->GetText()标签下文本
#include <iostream>
#include <sstream>
using namespace std;
#include "tinyxml.h"
int main() {
printf ("\\n** UTF-8 **\\n");
TiXmlDocument doc( "utf8test.xml" );
if (doc.LoadFile()) {
cout << " utf8test.xml :\\n" << endl;
doc.Print();
cout << endl;
} else {
printf( "WARNING: File 'utf8test.xml' not found.\\n"
"(Are you running the test from the wrong directory?)\\n"
"Could not test UTF-8 functionality.\\n" );
return 0;
}
TiXmlHandle docH( &doc );
// Get the attribute "value" from the "Russian" element and check it.
TiXmlElement* element = docH.FirstChildElement( "document" ).FirstChildElement( "English" ).Element();
TiXmlAttribute* attribute = element->FirstAttribute();
for (; attribute != NULL; attribute = attribute->Next() ) {
cout << attribute->Name() << " : " << attribute->Value() << endl;
}
cout << " English : " << element->GetText() << endl;
return 0;
}
以上是关于tinyxml使用的主要内容,如果未能解决你的问题,请参考以下文章