Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

xml parsing


May 21, 2021 CrossApp



CrossAppy has added a tinyxml2 library for xml resolution. #include "CrossApp.h" already contains tinyxml2.h without the need to introduce header files, so that we can easily generate and parse xml files during development.


xml document generation


Namespace

using namespace tinyxml2;

Build xml code

    //获得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;


The XML is generated as follows:

<?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>


Resolve XML


Let's parse the XML document generated above

Parsing the code:

    //解析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;

Print results:

attribute_name = version,attribute_value = 1.0
 
keyEle Text= Text
 
childEle Text= W3Cschool
 
childEle Text= W3Cschool
 
childEle Text= W3Cschool

Attention:

Tinyxml will have problems parsing the assert folder on android in the following ways:

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);