Possible Duplicate:
how to retrieve element value of XML using Java?
I am trying to parse a xml file with java using dom.
The xml looks like this:
<documents>
<document>
<element name="doctype">
<value>Circular</value>
</element>
</document>
</documents>
Geting the root node “documents” and also the child node “document” is easy. But I can’t get the value of the element with the name “doctype” (I want to store the value “Circular” in a database). Can someone help me please?
You could use the following XPath to get the data you are looking for:
Demo
The following demo code demonstrates how to execute that XPath against your DOM. This application will run in any standard Java SE 5 (or newer) install.
input.xml
I have expanded the XML document to contain
elementelements withnameattributes with values other thatdoctypeto demonstrate that the XPath works.Output
The result of executing the XPath is the very
Stringyou are looking for: