TinyXML2 解析器 c++ 奇怪的标签

TinyXML2 parser c++ strange tag

大家早上好,我要解析一个 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;
}

权限解析报错。你能帮我吗?非常感谢

您在此处获得 permissions(注意 "s")容器标签,其中包含大量 permission 标签。

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

那么您正在尝试访问它的 name 属性:

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

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

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