7

我想从TinyXml输出中解析一组元素。本质上,我需要挑选出任何端口元素的"portid"属性,该端口的状态为"open"(如下所示的端口 23)。

最好的方法是什么?以下是 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>
4

2 回答 2

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;
    }
于 2009-04-26T02:00:10.473 回答
4

除了 TinyXML,最好的办法是使用TinyXPath库。

这是我对正确XPath查询的最佳猜测:

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

您可以使用在线测试仪进行检查。

于 2009-05-02T14:48:01.327 回答