W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
CrossAppy已經(jīng)加入了tinyxml2庫用于xml解析。#include "CrossApp.h"時候已經(jīng)包含tinyxml2.h無須再引入頭文件,這樣我們就能在開發(fā)時方便的生成和解析xml文件。
xml文檔生成
命名空間
using namespace tinyxml2;
//獲得xml的保存路徑
std::string filePath = CCFileUtils::sharedFileUtils()->getWritablePath() + "test.xml";
//在生成一個XMLDocument對象
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
//xml 聲明(參數(shù)可選)
XMLDeclaration *pDel = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
//將pDel添加到XMLDocument對象中
pDoc->LinkEndChild(pDel);
//添加plist節(jié)點
XMLElement *plistElement = pDoc->NewElement("plist");
//設(shè)置l版本
plistElement->SetAttribute("version", "1.0");
//將pDec添加到XMLDocument對象中
pDoc->LinkEndChild(plistElement);
//添加一行注釋
XMLComment *commentElement = pDoc->NewComment("this is xml comment");
//將注釋添加到XMLDocument對象中
plistElement->LinkEndChild(commentElement);
//添加dic節(jié)點
XMLElement *dicElement = pDoc->NewElement("dic");
plistElement->LinkEndChild(dicElement);
//添加key節(jié)點
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();
//獲取第一個節(jié)點屬性
const XMLAttribute *attribute = rootEle->FirstAttribute();
//打印節(jié)點屬性名和值
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;
打印結(jié)果:
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);
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: