标签:
static void makeXml(const char *fileName)
{
std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
//xml 声明(参数可选)
XMLDeclaration *pDel = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
pDoc->LinkEndChild(pDel);
//添加plist节点
XMLElement *plistElement = pDoc->NewElement("plist");
plistElement->SetAttribute("version", "1.0");
pDoc->LinkEndChild(plistElement);
XMLComment *commentElement = pDoc->NewComment("this is xml comment");
plistElement->LinkEndChild(commentElement);
//添加dic节点
XMLElement *dicElement = pDoc->NewElement("dic");
plistElement->LinkEndChild(dicElement);
//添加key节点
XMLElement *keyElement = pDoc->NewElement("key");
keyElement->LinkEndChild(pDoc->NewText("Text"));
dicElement->LinkEndChild(keyElement);
XMLElement *arrayElement = pDoc->NewElement("array");
dicElement->LinkEndChild(arrayElement);
for (int i = 0; i<3; i++) {
XMLElement *elm = pDoc->NewElement("name");
elm->LinkEndChild(pDoc->NewText("Cocos2d-x"));
arrayElement->LinkEndChild(elm);
}
pDoc->SaveFile(filePath.c_str());
pDoc->Print();
delete pDoc;
}
static void parseXML(const char *fileName)
{
std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
XMLError errorId = pDoc->LoadFile(filePath.c_str());
if (errorId != 0) {
//xml格式错误
return;
}
XMLElement *rootEle = pDoc->RootElement();
//获取第一个节点属性
const XMLAttribute *attribute = rootEle->FirstAttribute();
//打印节点属性名和值
log("attribute<em>name = %s,attribute</em>value = %s", attribute->Name(), attribute->Value());
XMLElement *dicEle = rootEle->FirstChildElement("dic");
XMLElement *keyEle = dicEle->FirstChildElement("key");
if (keyEle) {
log("keyEle Text= %s", keyEle->GetText());
}
XMLElement *arrayEle = keyEle->NextSiblingElement();
XMLElement *childEle = arrayEle->FirstChildElement();
while ( childEle ) {
log("childEle Text= %s", childEle->GetText());
childEle = childEle->NextSiblingElement();
}
delete pDoc;
}
Mini-XML is a small XML library that you can use to read and write XML and XML-like data files in your application without requiring large non-standard libraries. Mini-XML only requires an ANSI C compatible compiler (GCC works, as do most vendors‘ ANSI C compilers) and a ‘make‘ program. Mini-XML supports reading of UTF-8 and UTF-16 and writing of UTF-8 encoded XML files and strings. Data is stored in a linked-list tree structure, preserving the XML data hierarchy, and arbitrary element names, attributes, and attribute values are supported with no preset limits, just available memory.
标签:
原文地址:http://blog.csdn.net/ubuntu64fan/article/details/42743269