1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

matching and getting xml data

Discussion in 'Java' started by Juuno, Apr 22, 2009.

  1. Juuno

    Juuno New Member

    Feb 3, 2009
    Likes Received:
    Trophy Points:
    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:

          <title>XML Developer's Guide</title>
          <author>Gambardella, Matthew</author>
          <title>Midnight Rain</title>
          <author>Ralls, Kim</author>
          <title>Hello World</title>
          <author>Richard, Kim</author>
          <author>Smith, Joe</author>
    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.

                    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
                    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

Share This Page