Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Failing to extract xml value using Jdom & Xpath

ziggy25Sep 3 2011 — edited Sep 9 2011
I have a method (getSingleNodeValue()) which when passed an xpatch expression will extract the value of the specified element in the xml document refered to in 'doc'. Assume doc at this point has been initialised as shown below and xmlInput is the buffer containing the xml content.
    SAXBuilder	builder		=	null;
    Document	doc		=	null; 
    XPath		xpathInstance	= 	null;
    
    doc	= builder.build(new StringReader(xmlInput));
When i call the method, i pass the following xpath xpression
    /TOP4A/PERLODSUMDEC/TINPLD1/text()
Here is the method. It basically just takes an xml buffer and uses xpath to extract the value:
    public static String getSingleNodeValue(String xpathExpr) throws Exception{
    
    	Text list = null;
    	
    	try {
    		xpathInstance = XPath.newInstance(xpathExpr);
    		list = (Text) xpathInstance.selectSingleNode(doc);
    	} catch (JDOMException e) {
    		throw new Exception(e);
    	}catch (Exception e){
    		throw new Exception(e);
    	} 
    	
    	return list==null ? "?" : list.getText();
    }
The above method always returns "?" i.e. nothing is found so 'list' is null.
The xml document it looks at is
    <TOP4A xmlns="http://www.testurl.co.uk/enment/gqr/3232/1">    
      <HEAD>
        <Doc>ABCDUK1234</Doc>  
      </HEAD>    
      <PERLODSUMDEC>
        <TINPLD1>10109000000000000</TINPLD1>
      </PERLODSUMDEC>
    </TOP4A>
The same method works with other xml documents so i am not sure what is special about this one. There is no exception so the xml is valid xml. Its just that the method always sets 'list' to null. Any ideas?


Edit
------

Here is a running program testing the above:
    import org.jdom.*;
    import org.jdom.input.*;
    import org.jdom.xpath.*;
    
    import java.io.IOException;
    import java.io.StringReader;
    
    public class XpathTest {
    	
    	
    	public static String getSingleNodeValue(String xpathExpr, String xmlInput) throws Exception{
    		
    	    Text list = null;
    		SAXBuilder  builder 		=   null;
    		Document    doc				=   null; 
    		XPath       xpathInstance   =   null;
    			    
    	    try {
    	    	builder	= new SAXBuilder();	
    	    	doc		= builder.build(new StringReader(xmlInput));
    	    	
    	        xpathInstance = XPath.newInstance(xpathExpr);
    	        list = (Text) xpathInstance.selectSingleNode(doc);
    	    } catch (JDOMException e) {
    	        throw new Exception(e);
    	    }catch (Exception e){
    	        throw new Exception(e);
    	    } 
    
    	    return list==null ? "Nothing Found" : list.getText();
    	}
    	
    	public static void main(String[] args){
    		
    		String xmlInput1 = "<TOP4A xmlns=\"http://www.testurl.co.uk/enment/gqr/3232/1\"><HEAD><Doc>ABCDUK1234</Doc></HEAD><PERLODSUMDEC><TINPLD1>10109000000000000</TINPLD1></PERLODSUMDEC></TOP4A>";
    		String xpathExpr = "/TOP4A/PERLODSUMDEC/TINPLD1/text()";
    				
    		XpathTest xp = new XpathTest();
    		try {
    			System.out.println(xp.getSingleNodeValue(xpathExpr, xmlInput1));
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
When i run the above, the output is
    Nothing found
Edit
-----

I have run some further testing and it appears that if i remove the namespace url it does work. Not sure why yet. Is there any way i can tell it to ignore the namespace?

Edited by: ziggy on Sep 3, 2011 4:57 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 7 2011
Added on Sep 3 2011
4 comments
693 views