XML parsing problems

有些话、适合烂在心里 提交于 2019-12-25 18:54:14

问题


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>

回答1:


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




回答2:


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.




回答3:


You can use jdom,it had a simple api,is easy to use.




回答4:


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.



来源:https://stackoverflow.com/questions/1795046/xml-parsing-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!