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