2009-04-25 7 views
7

Chciałbym parsować grupę elementów z wyjścia TinyXml. Zasadniczo, muszę wybrać dowolny element portu elementu "portid" atrybut portu ma stan "open" (pokazany poniżej dla portu 23).Jak używać TinyXml do analizowania dla konkretnego elementu?

Jaki jest najlepszy sposób na zrobienie tego? Oto (uproszczony) witryny wyjściu z TinyXML:

<?xml version="1.0" ?> 
<nmaprun> 
    <host> 
     <ports> 
      <port protocol="tcp" portid="22"> 
       <state state="filtered"/> 
      </port> 
      <port protocol="tcp" portid="23"> 
       <state state="open "/> 
      </port> 
      <port protocol="tcp" portid="24"> 
       <state state="filtered" /> 
      </port> 
      <port protocol="tcp" portid="25"> 
       <state state="filtered" /> 
      </port> 
      <port protocol="tcp" portid="80"> 
       <state state="filtered" /> 
      </port> 
     </ports> 
    </host> 
</nmaprun> 

Odpowiedz

10

Będzie to mniej więcej to zrobić:

TiXmlHandle docHandle(&doc); 

    TiXmlElement* child = docHandle.FirstChild("nmaprun").FirstChild("host").FirstChild("ports").FirstChild("port").ToElement(); 

    int port; 
    string state; 
    for(child; child; child=child->NextSiblingElement()) 
    { 

     port = atoi(child->Attribute("portid")); 

     TiXmlElement* state_el = child->FirstChild()->ToElement(); 

     state = state_el->Attribute("state"); 

     if ("filtered" == state) 
      cout << "port: " << port << " is filtered! " << endl; 
     else 
      cout << "port: " << port << " is unfiltered! " << endl; 
    } 
4

Najprościej jest użyć biblioteki TinyXPath oprócz TinyXML.

To jest mój najlepszy przypuszczenie na prawej XPath zapytania:

/nmaprun/host/ports/port[state/@state="open"][1]/@portid

Można sprawdzić to z online tester.