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?

Community
  • 1
  • 1
Metalhead89
  • 1,740
  • 9
  • 30
  • 58
  • 3
    Why can you not get that element? Show us your code. – Jesper Jul 20 '12 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. – Metalhead89 Jul 20 '12 at 12:03
  • Sorry but this did not really help – Metalhead89 Jul 20 '12 at 12:06
  • If I wirite this: System.out.println(doc.getElementsByTagName("data").item(0).getChildNodes().item(0).getNodeValue()); A Nullpointerexeption comes out – Metalhead89 Jul 20 '12 at 12:09
  • I recommend using the XPath APIs included in Java SE 5 instead of the DOM APIs: http://stackoverflow.com/a/4077986/383861 – bdoughan Jul 20 '12 at 12:16
  • I am sorry but I dont want to restrcuture my whole project. Maybe this is easier but isnt there an easy way with DOM? – Metalhead89 Jul 20 '12 at 12:22
  • You can issue the XPath statement right against the DOM you currently have. No need to restructure anything. – bdoughan Jul 20 '12 at 12:25
  • I tried this: XPath xPath = XPathFactory.newInstance().newXPath(); Node node = null; try { node = (Node) xPath.evaluate("/element/doctype", doc, XPathConstants.NODE); } catch (XPathExpressionException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(node.getNodeValue()); But then I get a nullpointer exeption:( – Metalhead89 Jul 20 '12 at 12:39

1 Answers1

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
bdoughan
  • 147,609
  • 23
  • 300
  • 400