English 中文(简体)
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.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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签