matching and getting xml data

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

  1. Juuno

    Juuno New Member

    Joined:
    Feb 3, 2009
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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:
    <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
                                               
                        }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice