How to use TinyXml to parse for a specific element

给你一囗甜甜゛ 提交于 2019-12-05 05:41:49

This will roughly do it:

    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;
    }
Jim Hunziker

Your best bet is to use the TinyXPath library in addition to TinyXML.

This is my best guess for the right XPath query:

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

You can check it with an online tester.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!