Search code examples
c++xmlparsingtinyxml2

TinyXML2 parser c++ strange tag


Good morning everyone, I have to parse an XML file and I decided to use tinyxml2. I did something and then I stopped because I don't know how can I parse this.

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

My problem is with the tag <permissionbecause it's not a child of permissions, neither an attribute. This is the c++ code of my parser

#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;
}

The permission parsing gives me an error. Can you help me please? Thank you very much


Solution

  • You are getting the permissions (note the "s") container tag here, which, in turn, contains a lot of permission tags inside.

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

    Then you are trying to access it's name attribute:

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

    However, permissions (note the "s" again) doesn't have any attributes, it's each of the permission elements that contain "name". Instead you should iterate over the permission elements inside permissions, and get the "name" attribute for each of them:

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