8

I've got a section of XML that looks like this:

<entry>
<id>tag:example.com,2005:Release/343597</id>
<published>2012-04-10T11:29:19Z</published>
<updated>2012-04-10T12:04:41Z</updated>
<link type="text/html" href="http://example.com/projects/example1" rel="alternate"/>
<title>example1</title>
</entry>

I need to grab the link http://example.com/projects/example1 from this block. I'm not sure how to do this. To get the title of the project I use this code:

String title1 = children.item(9).getFirstChild().getNodeValue();

where children is the getChildNodes() object for the <entry> </entry> block. But I keep getting NullPointerExceptions when I try to get the node value for the <link> node in a similar way. I see that the XML code is different for the <link> node, and I'm not sure what it's value is.... Please advise!

2
  • Do you need the XPath syntax for this? Or you need the Java API syntax? Commented Apr 10, 2012 at 18:31
  • 1
    Ahh, good question, looking for the Java API syntax. But I got it working, see below. Thanks
    – blaughli
    Commented Apr 10, 2012 at 19:01

2 Answers 2

15

The xpath expression to get that node is

//entry/link/@href

In java you can write

Document doc = ... // your XML document
XPathExpression xp = XPathFactory.newInstance().newXPath().compile("//entry/link/@href");
String href = xp.evaluate(doc);

Then if you need to get the link value of the entry with a specific id you can change the xpath expression to

//entry[id='tag:example.com,2005:Release/343597']/link/@href

Finally if you want to get all the links in the documents, if the document has many entry elements you can write

Document doc = ... // your XML document
XPathExpression xp = XPathFactory.newInstance().newXPath().compile("//entry/link/@href");
NodeList links = (NodeList) xp.evaluate(doc, XPathConstants.NODESET);
// and iterate on links
2
  • 1
    Thanks dash1e, you pushed me in the right direction. Using the syntax you provided I was able to evaluate the compiled Xpath expression (returning the results as a NODESET). I then made a NodeList from the result (the NODESET), and then I iterated through with a while loop to grab all of the links from the RSS page I'm reading. Thank you very much :).
    – blaughli
    Commented Apr 10, 2012 at 19:03
  • I add to the answer the NodeList example too. For completeness.
    – dash1e
    Commented Apr 10, 2012 at 19:10
6

Here is the complete code:

    DocumentBuilderFactory domFactory = DocumentBuilderFactory
            .newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("test.xml");
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("//entry/link/@href");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
         System.out.println(nodes.item(i));
    }
1
  • Thanks Phani, that's exactly what I did.
    – blaughli
    Commented Apr 10, 2012 at 19:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.