xml解析

2018-09-08 17:23 更新

CrossAppy已經(jīng)加入了tinyxml2庫用于xml解析。#include "CrossApp.h"時候已經(jīng)包含tinyxml2.h無須再引入頭文件,這樣我們就能在開發(fā)時方便的生成和解析xml文件。


xml文檔生成


命名空間

using namespace tinyxml2;

生成xml代碼

    //獲得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);


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號