我们现在生成一个create.xml
@H_502_3@@H_502_3@
@H_502_3@@H_502_3@
<tipch@H_502_3@="tiptiptip@H_502_3@"@H_502_3@en@H_502_3@="a tip@H_502_3@"@H_502_3@>@H_502_3@Text@H_502_3@</tip>@H_502_3@@H_502_3@
@H_502_3@@H_502_3@
1、引入头文件
#include "support/tinyxml2/tinyxml2.h"
using namespace tinyxml2;
tinyxml2::XMLDocument * doc=new tinyxml2::XMLDocument();
//声明@H_502_3@
@H_502_3@
/*tinyxml2::XMLDeclaration * dec=doc->NewDeclaration("<?xml version='1.0' encoding='UTF-8' ?>");@H_502_3@
@H_502_3@
doc->LinkEndChild(dec);*/@H_502_3@
@H_502_3@
tinyxml2::XMLElement * eleRoot=doc->NewElement("language");@H_502_3@
@H_502_3@
eleRoot->SetAttribute("name","CH");@H_502_3@
@H_502_3@
doc->LinkEndChild(eleRoot);@H_502_3@
@H_502_3@
//注释@H_502_3@
@H_502_3@
tinyxml2::XMLComment * com=doc->NewComment("this is a xml file!");@H_502_3@
@H_502_3@
eleRoot->LinkEndChild(com);@H_502_3@
tinyxml2::XMLElement * ele0=doc->NewElement("tip");@H_502_3@
@H_502_3@
ele0->SetAttribute("ch","tiptiptip");@H_502_3@
@H_502_3@
ele0->SetAttribute("en","a tip");@H_502_3@
@H_502_3@
ele0->LinkEndChild(doc->NewText("Text"));@H_502_3@
@H_502_3@
eleRoot->LinkEndChild(ele0);@H_502_3@
string path=CCFileUtils::sharedFileUtils()->getWritablePath()+"create.xml";@H_502_3@
@H_502_3@
doc->SaveFile(path.c_str());@H_502_3@
@H_502_3@
delete doc;@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
tinyxml2::XMLDocument * doc=new tinyxml2::XMLDocument();
string path=CCFileUtils::sharedFileUtils()->getWritablePath()+"create.xml";
tinyxml2::XMLError error=doc->LoadFile(path.c_str());
if(error!=tinyxml2::XML_SUCCESS){
@H_502_3@return "No corresponding data found";@H_502_3@;
}
//根元素
tinyxml2::XMLElement * eleRoot=doc->RootElement();
CCLog( "create.xml eleRoot>> name=%s,value=%s",eleRoot->Name(),eleRoot->Value()@H_502_3@);
string path=CCFileUtils::sharedFileUtils()->getWritablePath()+"create.xml";
tinyxml2::XMLError error=doc->LoadFile(path.c_str());
if(error!=tinyxml2::XML_SUCCESS){
@H_502_3@return "No corresponding data found";@H_502_3@;
}
//根元素
tinyxml2::XMLElement * eleRoot=doc->RootElement();
CCLog( "create.xml eleRoot>> name=%s,value=%s",eleRoot->Name(),eleRoot->Value()@H_502_3@);
//根元素的属性
const tinyxml2::XMLAttribute * attRoot=eleRoot->FirstAttribute();
CCLog( "create.xml attRoot>> name=%s,attRoot->Name(),attRoot->Value()@H_502_3@);
// 根元素的@H_502_3@子元素
tinyxml2::XMLElement * ele0=eleRoot->FirstChildElement();
CCLog( "create.xml ele0>> name=%s,value=%s,content=%s",ele0->Name(),ele0->Value(),ele0->FirstChild()->Value()@H_502_3@);
//子元素属性
const tinyxml2::XMLAttribute * att0=ele0->FirstAttribute();
CCLog( "create.xml att0>> name=%s,att0->Name(),att0->Value()@H_502_3@);
const tinyxml2::XMLAttribute * attRoot=eleRoot->FirstAttribute();
CCLog( "create.xml attRoot>> name=%s,attRoot->Name(),attRoot->Value()@H_502_3@);
// 根元素的@H_502_3@子元素
tinyxml2::XMLElement * ele0=eleRoot->FirstChildElement();
CCLog( "create.xml ele0>> name=%s,value=%s,content=%s",ele0->Name(),ele0->Value(),ele0->FirstChild()->Value()@H_502_3@);
//子元素属性
const tinyxml2::XMLAttribute * att0=ele0->FirstAttribute();
CCLog( "create.xml att0>> name=%s,att0->Name(),att0->Value()@H_502_3@);