Extract Data from XML in Java
I have this xml data format. 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>
开发者_如何学Python</children>
</xDiagram>
Can anyone suggest a better way to do it? I like to use XPath but it can't lock on the element.
Thanks
XPath is the right tool for this type of job. You can try improving your XPath expression at a site like:
http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm
For example, this XPath will return a NodeList containing all of the inner "Properties" elements:
/xDiagram/children/properties/properties
The Java Streaming API for XML is also handy (when SAX, DOM, or XPath aren't quite right for the job).
See if JAXB helps you: http://www.roseindia.net/jaxb/r/jaxb.shtml
精彩评论