我想从 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>

最佳答案

这将大致做到这一点:

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

关于c++ - 如何使用 TinyXml 解析特定元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/789817/

10-11 02:44
查看更多