本文介绍了如何使用TinyXml解析特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从TinyXml
输出中解析出一组元素.本质上,我需要选择端口状态为"open"
(端口23如下所示)的任何端口元素的"portid"
属性.
I would like to parse a group of elements out of a TinyXml
output. Essentially, I need to pick out any port element's "portid"
attribute of the port has a state of "open"
(shown below for port 23).
执行此操作的最佳方法是什么?这是TinyXml的输出的(简化的)清单:
What's the best way to do this? Here's the (simplified) listing for the output from 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;
}
这篇关于如何使用TinyXml解析特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!