2009-04-25 14 views
7

TinyXml出力から要素のグループを解析したいと考えています。基本的には、ポートの任意のポート要素の"portid"属性の状態が"open"(ポート23の場合は以下の状態)であることを選択する必要があります。TinyXmlを使用して特定の要素を解析する方法

これを行うにはどうすればよいですか?ここで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> 

答えて

10

これは大体それを行います。

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

あなたの最善の策はTinyXMLに加えてTinyXPathライブラリを使用することです。

これは右XPathクエリのための私の最高の推測です:

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

あなたはonline testerでそれを確認することができます。

関連する問題