0

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

ScazzoMatto
  • 99
  • 1
  • 4
  • 12

1 Answers1

0

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");
}
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • It doesn't work, it always gives the same error message – ScazzoMatto Oct 06 '15 at 08:28
  • @ScazzoMatto, which error message exactly? you should update your post with what you've tried and what error you're getting. – SingerOfTheFall Oct 06 '15 at 08:50
  • It was my fault because permissions is a child of app. Now it works! thanks a lot. One more question: I have to open 100 files sequentially using doc.LoadFile("name1.xml), doc.LoadFile("name2.xml"). how can i open them with just one command? (like a for) – ScazzoMatto Oct 06 '15 at 09:50
  • @Scazzo, probably you need something like `for(int i = 0; i < 100; ++i) { std::stringstream sstm; sstm << "file" << i << ".txt"; std::string filename = sstm.str(); }` – SingerOfTheFall Oct 06 '15 at 09:55