Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   matching and getting xml data (http://www.go4expert.com/forums/matching-getting-xml-data-t17043/)

Juuno 22Apr2009 20:12

matching and getting xml data
 
I would like to match a text node of an xml file with the user input data and if it is matched, then retrieve the other text node which is the next sibling of that text node. For example, my xml file is like:

HTML Code:

<catalog>
<book>
      <title>XML Developer's Guide</title>
      <author>Gambardella, Matthew</author>
</book>
  <book>
      <title>Midnight Rain</title>
      <author>Ralls, Kim</author>
</book>
<book>
      <title>Hello World</title>
      <author>Richard, Kim</author>
      <author>Smith, Joe</author>
</book>
</catalog>

Then, my program will match whether the user input is matched with the title of the book and if matched, it will return the author of the book. And say if there's two authors , then it will retrieve all authors' name. How can I do it. Here is the code which retrieve and match the keyword and it works. But I don't know how to retrieve next text node if matched. Could you kindly help me? I try to get with getNextSibling, but i can't. So,please help me.

Code:

                DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
                dFactory.setNamespaceAware(true);
                DocumentBuilder builder = domFactory.newDocumentBuilder();
                Document doc = builder.parse(xmlbookfile);

                XPathFactory factory = XPathFactory.newInstance();
                XPath xpath = factory.newXPath();
                XPathExpression expr = xpath.compile("//book/title/text()");

                Object result = expr.evaluate(doc, XPathConstants.NODESET);
                NodeList nodes = (NodeList) result;
               
       
                for (int i = 0; i < nodes.getLength(); i++) {
                      String bname= nodes.item(i).getNodeValue();
                    if (keyword.equalsIgnoreCase(userinput)){
                                    // i would like to retrieve the author text node
                                         
                    }



All times are GMT +5.5. The time now is 18:19.