2009-04-25 5 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으로 확인할 수 있습니다.

관련 문제