XML parsing problems
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开发者_如何学Python.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>
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@*");
which would give all the attributes of all the MMV attributes. Similarly
Nodes attributes = document.query("//CS@*");
UPDATE after XML was posted
Node valAttribute = document.query("MMV@val").get(0);
and the CS version should still work or
Nodes csAttributes = document.query("MMV/CS@*");
Alternatively this could be done with XSLT.
NOTE: You ask for the attributes; you may actually want only the attribute values
You can use DOM/SAX/Pull Parser to extract the required information. The choice depends upon the size of XML and what you want to do.
You can use jdom,it had a simple api,is easy to use.
Try JAXB. It will parse the XML and bind the attributes to objects. You can create an XSD from the XML and JAXB will generate the class files and do the parsing.
精彩评论