Results 1 to 7 of 7
  1. #1
    Juuno is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default 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:

    XML 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.

    Java 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
    	    				   				
    	    			}
    Last edited by Juuno; 04-22-2009 at 04:50 PM.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    What is keyword? It is not declared here.

    Try getFirstChild() and/or getTextContent() on the title and author elements.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Juuno is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    Sorry. I made some changes to my code and i didn't check it well before posting it. That's my fault. It's not keyword. It's the variable 'bname'.

    I still can't figure it out.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    did you try getTextContent() ?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    Juuno is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    yeah.. i tried both of the following:


    val = nodes.item(i).getNextSibling().getNodeValue().toSt ring() + '\n';

    and

    val = nodes.item(i).getNextSibling().getTextContent() + '\n';

    where val is declared as static String variable and.. there's an error: java.lang.NullPointerException

    And the problem is I don't know how to get to the next node.

  6. #6
    Juuno is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    Here is the code I tried, no more error but i still can't get the author name. Why? I am thinking... is it because I used xpath? or.... really appreciate for ur reply.

    Java Code:
    public String testMethod(String name) {
          
       	File indexfile = new File("C:\\books.xml");
        	
        	{
    	    	try{
    	    		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    	    		domFactory.setNamespaceAware(true); 
    	    		DocumentBuilder builder = domFactory.newDocumentBuilder();
    	    		Document doc = builder.parse(indexfile);
    
    	    		XPathFactory factory = XPathFactory.newInstance();
    	    		XPath xpath = factory.newXPath();
    	    		XPathExpression expr = xpath.compile("//catalog/name/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();
    					//name is the string i got as an input
    					//return is the string i will return 'a book name and an author'
    	    			if (bname.equalsIgnoreCase(name)){
    	    				returnvalue = returnvalue + bname + '\n';
    	    			
    						//this is what i try to move to next node "author" to get the text content
    	    				while (nodes.item(i).getNextSibling() != null){
    	    					returnvalue = returnvalue + nodes.item(i).getNextSibling().getNodeValue().toString() + '\n';
    	    				} 				   				
    	    			}    		
    	    			
    	    		}
    	    	}catch(Exception e){return e.toString();}
           }
        	return returnvalue.toString();
      }

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Why are you using getNextSibling()? Author is a child of book, not a sibling.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Regex - matching literal characters
    By racha0601 in forum Advanced Java
    Replies: 3
    Last Post: 04-07-2009, 11:25 PM
  2. regular expressions and string matching
    By DennyLoi in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 10:15 AM
  3. Help with signature matching
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •