XML 解析问题 [英] XML parsing problems
本文介绍了XML 解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要解析这个,这样我才能得到MMV的属性和所有CS标签的所有属性
i need to parse this such that i can get the attribute of MMV and all the attributes of all CS tags
<MMV val="Configdes000110010101">
<CS protocol="SNMP" CommandString="wmanIfBsDcdInterval"
oid="1.3.6.1.2.1.10.184.1.1.2.2.1.1" Get_SecurityString="public"
Set_SecurityString="public" type="INTEGER" > </CS>
<CS protocol="SNMP" CommandString="wmanIfBsUcdInterval"
oid="1.3.6.1.2.1.10.184.1.1.2.2.1.2" Get_SecurityString="public"
Set_SecurityString="public" type="INTEGER" > </CS>
<CS protocol="SNMP" CommandString="wmanIfBsUcdTransition"
oid="1.3.6.1.2.1.10.184.1.1.2.2.1.3" Get_SecurityString="public"
Set_SecurityString="public" type="INTEGER" > </CS>
<CS protocol="SNMP" CommandString="wmanIfBsDcdTransition"
oid="1.3.6.1.2.1.10.184.1.1.2.2.1.4" Get_SecurityString="public"
Set_SecurityString="public" type="INTEGER" > </CS>
</MMV>
推荐答案
您将需要一个 XML 解析器,最好是一个支持 XPath 的引擎.我使用 XOM (Java) http://www.xom.nu 并且会写一个类似于
you will need an XML parser and preferably an engine that supports XPath. I use XOM (Java) http://www.xom.nu and would write an XPath expression something like
Nodes attributes = document.query("//MMV@*");
这将给出所有 MMV 属性的所有属性.同理
which would give all the attributes of all the MMV attributes. Similarly
Nodes attributes = document.query("//CS@*");
发布 XML 后的更新
Node valAttribute = document.query("MMV@val").get(0);
并且 CS 版本应该仍然有效或
and the CS version should still work or
Nodes csAttributes = document.query("MMV/CS@*");
这也可以通过 XSLT 来完成.
Alternatively this could be done with XSLT.
注意:您要求的是属性;您实际上可能只想要属性值
NOTE: You ask for the attributes; you may actually want only the attribute values
这篇关于XML 解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文