views:

471

answers:

2

Hi guys,

I want to read XML data using XPath in Java, so for the information I have gathered I am not able to parse XML according to my requirement.

here is what I want to do:

Get XML file from online via its URL, then use XPath to parse it, I want to create two methods in it. One is in which I enter a specific node attribute id, and I get all the child nodes as result, and second is suppose I just want to get a specific child node value only

<?xml version="1.0"?>
<howto>
  <topic name="Java">
      <url>http://www.rgagnonjavahowto.htm&lt;/url&gt;
  <car>taxi</car>
  </topic>
  <topic ame="PowerBuilder">
       <url>http://www.rgagnon/pbhowto.htm&lt;/url&gt;
       <url>http://www.rgagnon/pbhowtonew.htm&lt;/url&gt;
  </topic>
  <topic name="Javascript">
        <url>http://www.rgagnon/jshowto.htm&lt;/url&gt;
  </topic>
 <topic name="VBScript">
       <url>http://www.rgagnon/vbshowto.htm&lt;/url&gt;
 </topic>
 </howto>

In above example I want to read all the elements if I search via @name and also one function in which I just want the url from @name 'Javascript' only return one node element.

I hope I cleared my question :)

Thanks.

Kai

+2  A: 

You need something along the lines of this:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(<uri_as_string>);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(<xpath_expression>);

Then you call expr.evaluate() passing in the document defined in that code and the return type you are expecting, and cast the result to the object type of the result.

If you need help with a specific XPath expressions, you should probably ask it as separate questions (unless that was your question in the first place here - I understood your question to be how to use the API in Java).

Edit: (Response to comment): This XPath expression will get you the text of the first URL element under PowerBuilder:

   /howto/topic[@name='PowerBuilder']/url/text()

This will get you the second:

  /howto/topic[@name='PowerBuilder']/url[2]/text()

You get that with this code:

  expr.evaluate(doc, XPathConstants.STRING);

If you don't know how many URLs are in a given node, then you should rather do something like this:

    XPathExpression expr = xpath.compile("/howto/topic[@name='PowerBuilder']/url");
    NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

And then loop over the NodeList.

Yishai
Thanks Yishai,I clear my question more, if I want to get a particular value from child nodes on a parent node. how that will be done?thanks
kaibuki
A: 

You can use the Java XPath API to do your parsing. If you are already aware of Xpath, have a look at this tutorial: http://www.ibm.com/developerworks/library/x-javaxpathapi.html

Abdel Olakara