简体   繁体   English

XML解析问题

[英]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的属性和所有CS标签的所有属性

<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. 您将需要一个XML解析器,最好是一个支持XPath的引擎。 I use XOM (Java) http://www.xom.nu and would write an XPath expression something like 我使用XOM(Java) http://www.xom.nu并会编写类似以下内容的XPath表达式

Nodes attributes = document.query("//MMV@*");

which would give all the attributes of all the MMV attributes. 这将提供所有MMV属性的所有属性。 Similarly 相似地

Nodes attributes = document.query("//CS@*");

UPDATE after XML was posted XML发布后的更新

Node valAttribute = document.query("MMV@val").get(0); 

and the CS version should still work or 并且CS版本仍然可以使用

Nodes csAttributes = document.query("MMV/CS@*");

Alternatively this could be done with XSLT. 或者,可以使用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. 您可以使用DOM / SAX / Pull Parser提取所需的信息。 The choice depends upon the size of XML and what you want to do. 选择取决于XML的大小以及您想做什么。

您可以使用jdom,它具有一个简单的api,易于使用。

Try JAXB. 试试JAXB。 It will parse the XML and bind the attributes to objects. 它将解析XML并将属性绑定到对象。 You can create an XSD from the XML and JAXB will generate the class files and do the parsing. 您可以从XML创建XSD,并且JAXB将生成类文件并进行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM