[jdom-interest] Can't get values using xpath

Abhijeet Sarwate asarwate at elm.mt.att.com
Fri Oct 24 09:40:50 PDT 2003


Folks

I use Jaxen's XPath engine to  get data out of xml

here is the xml that I use

<?xml version='1.0' encoding='ISO-8859-1'?>
    <APIInterface xmlns='http://nc3pc4.sch.advantis.com/XMLSchema/API' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'        
xsi:schemaLocation='http://nc3pc4.sch.advantis.com/XMLSchema/API 
./NC3API.xsd'>
    <StoreSiteResponse>
        <ReturnCode>00000000</ReturnCode>
        <NC3Key>22741892</NC3Key>
    </StoreSiteResponse>
</APIInterface>";


And here is the code I use to retrieve the "ReturnCode" tag. But value 
obtained is null

Can anyone tell  what is the problem in the code  or else ?

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.jdom.JDOMException;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.*;
import org.xml.sax.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.jms.*;
import javax.naming.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.apache.log4j.NDC;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.*;
import org.xml.sax.*;
import java.io.StringReader;

/**
 * Created by IntelliJ IDEA.
 * User: Abhijeet Sarwate
 * Date: Sep 26, 2003
 * Time: 2:28:46 PM
 * To change this template use Options | File Templates.
 */
public class testXpath {
   
    public static String oneXml = "<?xml version='1.0' 
encoding='ISO-8859-1'?><APIInterface 
xmlns='http://nc3pc4.sch.advantis.com/XMLSchema/API' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
xsi:schemaLocation='http://nc3pc4.sch.advantis.com/XMLSchema/API 
./NC3API.xsd'><StoreSiteResponse><ReturnCode>00000000</ReturnCode><NC3Key>22741892</NC3Key></StoreSiteResponse></APIInterface>";
    public String testNullVal(String inpXml){
       
        try{
            InputSource isA = new InputSource(new StringReader(oneXml));
            SAXBuilder sbA = new SAXBuilder();
            org.jdom.Document pvcDocA = sbA.build(isA);
            String RET =getProperty(pvcDocA, 
"/APIInterface/StoreSiteResponse/ReturnCode");
            System.out.println("RET : " + RET);
            
            }catch(Exception e){
                e.printStackTrace();
            }

        return "test";
    }   
    private static String getProperty(Object requestDoc,
                      String xpath)
        throws JDOMException {
        System.out.println("object :: " + 
org.jdom.xpath.XPath.selectNodes(requestDoc, xpath));
        org.jdom.Element  e = (org.jdom.Element) 
org.jdom.xpath.XPath.selectSingleNode(requestDoc, xpath);
        if (e != null) {
            return e.getText();
        }
        return "";
    }
   
    private static String check(Object o) {
    if (o == null) {
        return "";
    }
    return (String) o;
    }
   
  
    public static void main(String[] args){
        testXpath x1 = new testXpath();
        x1.testNullVal(testXml);
    }
       
}


Thanks

abhijeet sarwate




More information about the jdom-interest mailing list