xml解析
CrossAppy已经加入了tinyxml2库用于xml解析。#include "CrossApp.h"时候已经包含tinyxml2.h无须再引入头文件,这样我们就能在开发时方便的生成和解析xml文件。
xml文档生成
命名空间
using namespace tinyxml2;
生成xml代码
//获得xml的保存路径
std::string filePath = CCFileUtils::sharedFileUtils()->getWritablePath() + "test.xml";
//在生成一个XMLDocument对象
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
//xml 声明(参数可选)
XMLDeclaration *pDel = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
//将pDel添加到XMLDocument对象中
pDoc->LinkEndChild(pDel);
//添加plist节点
XMLElement *plistElement = pDoc->NewElement("plist");
//设置l版本
plistElement->SetAttribute("version", "1.0");
//将pDec添加到XMLDocument对象中
pDoc->LinkEndChild(plistElement);
//添加一行注释
XMLComment *commentElement = pDoc->NewComment("this is xml comment");
//将注释添加到XMLDocument对象中
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("W3Cschool"));
arrayElement->LinkEndChild(elm);
}
pDoc->SaveFile(filePath.c_str());
CCLog("path:%s", filePath.c_str());
pDoc->Print();
delete pDoc;
生成XML如下:
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<!--this is xml comment-->
<dic>
<key>Text</key>
<array>
<name>W3Cschool </name>
<name>W3Cschool </name>
<name>W3Cschool </name>
</array>
</dic>
</plist>
解析XML
下面我们就来解析一下上面生成的XML文档
解析代码:
//解析xml的路径
std::string filePath = CCFileUtils::sharedFileUtils()->getWritablePath() + "test.xml";
//生成一个XMLDocument对象
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
//将xml文件读取到XMLDocument对象中
XMLError errorId = pDoc->LoadFile(filePath.c_str());
if (errorId != 0) {
//xml格式错误
return;
}
XMLElement *rootEle = pDoc->RootElement();
//获取第一个节点属性
const XMLAttribute *attribute = rootEle->FirstAttribute();
//打印节点属性名和值
CCLog("attribute_name = %s,attribute_value = %s", attribute->Name(), attribute->Value());
XMLElement *dicEle = rootEle->FirstChildElement("dic");
XMLElement *keyEle = dicEle->FirstChildElement("key");
if (keyEle) {
CCLog("keyEle Text= %s", keyEle->GetText());
}
XMLElement *arrayEle = keyEle->NextSiblingElement();
XMLElement *childEle = arrayEle->FirstChildElement();
while (childEle) {
CCLog("childEle Text= %s", childEle->GetText());
childEle = childEle->NextSiblingElement();
}
delete pDoc;
打印结果:
attribute_name = version,attribute_value = 1.0
keyEle Text= Text
childEle Text= W3Cschool
childEle Text= W3Cschool
childEle Text= W3Cschool
注意:
tinyxml在android上解析assert文件夹下会有问题,解决方式如下:
unsigned long nSize = 0;
std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("test.xml");
unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(), "rb", &nSize);
tinyxml2::XMLDocument xmlDoc;
xmlDoc.Parse((const char *)pBuffer);