简体   繁体   English

从Java中的XML提取数据

[英]Extract Data from XML in Java

I have this xml data format. 我有这种xml数据格式。 I would like to extract the properties name and value from it. 我想从中提取属性名称和值。 I have tried to get node by node but it is troublesome. 我试图逐个节点进行操作,但这很麻烦。

<?xml version="1.0"> encoding="ASCII"?> 
<xDiagram>  
  <children iD="1261435145010.0" location="Point(547,184)" 
    size="Dimension(102,140)" shapeType="TestInfoShape"
    modelEntityID="TestInfo.7">
    <properties>
      <properties name="desc" type="MultiLinesText"
        parent="this_comp1" parentName="multiLinesText"
        modelPropName="desc" value="create test user can access"/>
      <properties name="name" type="String" parent="this_comp2"
        parentName="text" modelPropName="name" value="testCase1"/>
    </properties>
  </children>
</xDiagram>

Can anyone suggest a better way to do it? 谁能建议一种更好的方法呢? I like to use XPath but it can't lock on the element. 我喜欢使用XPath,但是它不能锁定元素。

Thanks 谢谢

XPath is the right tool for this type of job. XPath是用于此类工作的正确工具。 You can try improving your XPath expression at a site like: 您可以在以下站点尝试改善XPath表达式:

http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm

For example, this XPath will return a NodeList containing all of the inner "Properties" elements: 例如,此XPath将返回一个包含所有内部“属性”元素的NodeList:

/xDiagram/children/properties/properties

用于XML的Java Streaming API也很方便(当SAX,DOM或XPath不太适合这项工作时)。

看看JAXB是否可以帮助您: http : //www.roseindia.net/jaxb/r/jaxb.shtml

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

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