#include <tinyxml2/tinyxml2.h>
using namespace tinyxml2;
//
创建文档对象
auto myTinyXMLDoc = new XMLDocument ();
创建根节点并连接到文档
auto rootElement = myTinyXMLDoc-> NewElement ( "information" );
myTinyXMLDoc-> LinkEndChild (rootElement);
创建 student 节点 并连接到根节点
auto stuElement = myTinyXMLDoc-> "student" );
rootElement-> LinkEndChild (stuElement);
person 并连接到 节点
auto personElement = myTinyXMLDoc-> "person" );
stuElement-> LinkEndChild (personElement);
为 节点设置属性
personElement-> SetAttribute ( "id" , "123" );
name auto nameElement = myTinyXMLDoc-> "name" );
personElement-> LinkEndChild (nameElement);
节点添加文本值
auto nameText = myTinyXMLDoc-> NewText ( "zhangsan" );
nameElement-> LinkEndChild (nameText);
sex auto sexElement = myTinyXMLDoc-> "sex" );
personElement-> LinkEndChild (sexElement);
auto sexText = myTinyXMLDoc-> "male" );
sexElement-> LinkEndChild (sexText);
保存 XML
myTinyXMLDoc-> SaveFile ( FileUtils :: getInstance ()-> fullPathForFilename ( "stuInfo.xml" ). c_str ());
打印 Print ();
删除 myTinyXMLDoc 对象
auto myTinyXMLDoc = new XMLDocument ();
创建根节点并连接到文档
auto rootElement = myTinyXMLDoc-> NewElement ( "information" );
myTinyXMLDoc-> LinkEndChild (rootElement);
创建 student 节点 并连接到根节点
auto stuElement = myTinyXMLDoc-> "student" );
rootElement-> LinkEndChild (stuElement);
person 并连接到 节点
auto personElement = myTinyXMLDoc-> "person" );
stuElement-> LinkEndChild (personElement);
为 节点设置属性
personElement-> SetAttribute ( "id" , "123" );
name auto nameElement = myTinyXMLDoc-> "name" );
personElement-> LinkEndChild (nameElement);
节点添加文本值
auto nameText = myTinyXMLDoc-> NewText ( "zhangsan" );
nameElement-> LinkEndChild (nameText);
sex auto sexElement = myTinyXMLDoc-> "sex" );
personElement-> LinkEndChild (sexElement);
auto sexText = myTinyXMLDoc-> "male" );
sexElement-> LinkEndChild (sexText);
保存 XML
myTinyXMLDoc-> SaveFile ( FileUtils :: getInstance ()-> fullPathForFilename ( "stuInfo.xml" ). c_str ());
打印 Print ();
删除 myTinyXMLDoc 对象
delete myTinyXMLDoc;
cocos2d: fullPathForFilename: No file found at stuInfo.xml. Possible missing file.
<information>
<student>
<person id="123">
<name>zhangsan</name>
<sex>male</sex>
</person>
</student>
<information>
<student>
<person id="123">
<name>zhangsan</name>
<sex>male</sex>
</person>
</student>
</information>
第二部分解析:
XMLDocument
();
myTinyXMLDoc-> Parse ( getStringFromFile ( 或者
// auto myTinyXMLDoc = new XMLDocument(FileUtils::getInstance()->fullPathForFilename("stuInfo.xml").c_str());
// myTinyXMLDoc->LoadFile(FileUtils::getInstance()->fullPathForFilename("stuInfo.xml").c_str());
获取根节点
RootElement ();
获取 auto stuElement = rootElement-> FirstChildElement ();
auto personElement = stuElement-> FirstChildElement ();
while (personElement) {
的属性
auto attributeInfo = personElement-> FirstAttribute ();
while (attributeInfo) {
获取所有属性
CCLOG ( "%s: %s:" ,attributeInfo-> Name (),93)">Value ());
attributeInfo = attributeInfo-> Next ();
}
name
auto nameElement = personElement-> FirstChildElement ();
"name: %s" ,nameElement-> GetText ());
sex
auto sexElement = nameElement-> NextSiblingElement ();
"sex: %s" ,sexElement-> GetText ());
查找下一节点
personElement = personElement-> NextSiblingElement ();
}
对象
delete myTinyXMLDoc;
myTinyXMLDoc-> Parse ( getStringFromFile ( 或者
// auto myTinyXMLDoc = new XMLDocument(FileUtils::getInstance()->fullPathForFilename("stuInfo.xml").c_str());
// myTinyXMLDoc->LoadFile(FileUtils::getInstance()->fullPathForFilename("stuInfo.xml").c_str());
获取根节点
RootElement ();
获取 auto stuElement = rootElement-> FirstChildElement ();
auto personElement = stuElement-> FirstChildElement ();
while (personElement) {
的属性
auto attributeInfo = personElement-> FirstAttribute ();
while (attributeInfo) {
获取所有属性
CCLOG ( "%s: %s:" ,attributeInfo-> Name (),93)">Value ());
attributeInfo = attributeInfo-> Next ();
}
name
auto nameElement = personElement-> FirstChildElement ();
"name: %s" ,nameElement-> GetText ());
sex
auto sexElement = nameElement-> NextSiblingElement ();
"sex: %s" ,sexElement-> GetText ());
查找下一节点
personElement = personElement-> NextSiblingElement ();
}
对象
delete myTinyXMLDoc;