0

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?

8
  • 3
    Why can you not get that element? Show us your code. Commented Jul 20, 2012 at 11:56
  • doc.getElementsByTagName("document").item(0).getNodeName() doc is my documentbuilder, with whom I parsed the xml file. If I print this, the knot "document" will be displayed. But I dont know how to get the element values. Commented Jul 20, 2012 at 12:03
  • Sorry but this did not really help Commented Jul 20, 2012 at 12:06
  • If I wirite this: System.out.println(doc.getElementsByTagName("data").item(0).getChildNodes().item(0).getNodeValue()); A Nullpointerexeption comes out Commented Jul 20, 2012 at 12:09
  • I recommend using the XPath APIs included in Java SE 5 instead of the DOM APIs: stackoverflow.com/a/4077986/383861 Commented Jul 20, 2012 at 12:16

1 Answer 1

1

You could use the following XPath to get the data you are looking for:

/documents/document/element[@name='doctype']/value

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.

package forum11578831;

import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.Document;

public class Demo {

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document dDoc = builder.parse("src/forum11578831/input.xml");

        XPath xPath = XPathFactory.newInstance().newXPath();
        String string = (String) xPath.evaluate("/documents/document/element[@name='doctype']/value", dDoc, XPathConstants.STRING);
        System.out.println(string);
    }

}

input.xml

I have expanded the XML document to contain element elements with name attributes with values other that doctype to demonstrate that the XPath works.

<documents>
  <document>
    <element name="foo">
      <value>FOO</value>
    </element>
    <element name="doctype">
      <value>Circular</value>
    </element>
    <element name="bar">
      <value>BAR</value>
    </element>
  </document>
</documents>

Output

The result of executing the XPath is the very String you are looking for:

Circular
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, not it works fine. You saved me a lot of time!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.