用Java从XML中提取数据

时间:2009-12-28 03:56:15

标签: java xml

我有这种xml数据格式。我想从中提取属性名称和值。我试图逐个节点地获取,但这很麻烦。

<?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>

有人能建议更好的方法吗?我喜欢使用XPath但它无法锁定元素。

由于

3 个答案:

答案 0 :(得分:3)

XPath是适合此类工作的正确工具。您可以尝试在以下网站上改进XPath表达式:

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

例如,此XPath将返回包含所有内部“Properties”元素的NodeList:

/xDiagram/children/properties/properties

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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