c++ - 如何使用 TinyXml 解析特定元素

标签 c++ xml tinyxml

我想从 TinyXml 输出中解析一组元素。本质上,我需要找出端口的任何端口元素的 "portid" 属性具有 "open" 状态(下面显示端口 23)。

执行此操作的最佳方法是什么?这是 TinyXml 输出的(简化的) list :

<?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/

相关文章:

c++ - 仅使用 <iostream> c++ 读取 .csv 文件并存储到数组中

xml - 如何将 xpath 返回的 xml 数组转换为 Postgres 中的 int 数组

c++ - 将双字节字符串路径加载到 TinyXML2 中

c++ - TinyXML #include 问题...使用库

c++ - C++ 中的 XML 序列化/反序列化

java - 在 Eclipse-ADT 的路径中找不到程序 “make”

c++ - 返回字符数组求和的结果

c++ - 右值引用在这里如何工作?

java - 使用 maven-jaxb2-plugin 获取 boolean 变量的 Getter

xml - 查找单个标签内n个相同的内部标签内容