TinyXML2解析器c ++奇怪标记

时间:2015-10-06 07:43:58

标签: c++ xml parsing tinyxml2

大家早上好,我必须解析XML文件,我决定使用tinyxml2。我做了一些事情,然后我停了下来因为我不知道怎么解析这个。

<report>
<file name="com.kristanix.android.mahjongsolitaireepic.apk" size="29743 KB" sha1="af054314883c177b2fd533fc907fe5492fc498db" md5="f70d0175469a417facd0e5539c15efdf"/>
<app name="com.kristanix.android.mahjongsolitaireepic.apk" package="com.kristanix.android.mahjongsolitaireepic" version="2.0" minAPI="9">
<permissions>
<permission name="factory_test" used="TRUE" required="FALSE"/>
<permission name="get_tasks" used="TRUE" required="FALSE"/>
<permission name="record_audio" used="TRUE" required="FALSE"/>
<permission name="use_credentials" used="TRUE" required="FALSE"/>
<permission name="wake_lock" used="TRUE" required="FALSE"/>
<permission name="vibrate" used="TRUE" required="FALSE"/>
<permission name="access_wifi_state" used="TRUE" required="FALSE"/>
<permission name="read_contacts" used="TRUE" required="FALSE"/>
<permission name="access_fine_location" used="TRUE" required="FALSE"/>
<permission name="access_network_state" used="TRUE" required="TRUE"/>
<permission name="internet" used="TRUE" required="TRUE"/>
</permissions>
</app>
<analysis>
<virusTotalScore>0/57</virusTotalScore>
<obfuscation>partially</obfuscation>
<appVerification>
Nothing to verify. Application is policy compliant.
</appVerification>
<codeReview>Not Available.</codeReview>
<note/>
</analysis>
</report>

我的问题是标记<permission,因为它不是权限的子项,也不是属性。这是我的解析器的c ++代码

#include <stdio.h>
#include "tinyxml2.h"
#include <iostream>
using namespace tinyxml2;

#ifndef XMLCheckResult
#define XMLCheckResult(a_eResult) if (a_eResult != XML_SUCCESS) { printf("Error: %i\n", a_eResult); return a_eResult; }
#endif

int main() {
    //carico file
    XMLDocument doc;
    doc.LoadFile("test.xml");

    //controllo file
    XMLError eResult = doc.LoadFile("test.xml");
    XMLCheckResult(eResult);

    //minAPI
    int minapi; 
     XMLElement* attributeminapi = doc.FirstChildElement("report")->FirstChildElement("app");
     attributeminapi->QueryIntAttribute("minAPI", &minapi);

     //virus total (è una stringa)
     const char* virustot = doc.FirstChildElement("report")->FirstChildElement("analysis")->FirstChildElement("virusTotalScore")->GetText();

     //obfuscation
     const char* obfuscation = doc.FirstChildElement("report")->FirstChildElement("analysis")->FirstChildElement("obfuscation")->GetText();

     //PERMISSIONS
     //factory test
     XMLElement* fact = doc.FirstChildElement("report")->FirstChildElement("permissions");
     std::string str = fact->Attribute("name");

     return 0;
}

权限解析给我一个错误。你能帮我吗?非常感谢你

1 个答案:

答案 0 :(得分:0)

您在此处获得了permissions(注意“s”)容器标记,而该标记又包含很多permission个标记。

 XMLElement* fact = doc.FirstChildElement("report")->FirstChildElement("permissions");

然后您尝试访问它的name属性:

std::string str = fact->Attribute("name");

但是,permissions(再次注意“s”)没有任何属性,它是包含“name”的每个permission元素。相反,您应该遍历permission内的permissions元素,并为每个元素获取“name”属性:

for (tinyxml2::XMLElement* child = fact->FirstChildElement(); child != NULL; child = child->NextSiblingElement())
{
    std::string str = child->Attribute("name");
}