dev@jaxb.java.net

JAXB XPATH question.

From: Robert Spychala <robs_at_visiscience.com>
Date: Tue, 3 Apr 2007 00:31:00 -0400

I'm having some issues running xpath queries against DOM trees
marshalled to org.wc3.dom.Document from JAXB beans.

I'm able to evaluate the XPATH fine if it is loaded using
DocumentBuilder, but it's failing for Document objects Marhalled by
JAXB.

Here is some code that can be used to illustrate the prob:

----------

public void test_xpath_DOM() {
        
        System.out.println("test_xpath_DOM start");
        
        try {
                DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document document = builder.parse(new
File(System.getProperty("tmp.path") + File.separator +
"../resources_1/snaplog-meta.xml"));
                
                OutputFormat format = new OutputFormat(document);
                XMLSerializer output = new XMLSerializer(System.out, format);
                output.serialize(document);
                
                System.out.println();

                XPath xpath = XPathFactory.newInstance().newXPath();
                
                NodeList nodeSet = (NodeList)
xpath.evaluate("/snaplog-meta/database-part[@from-ge-date =
'1997-07-16T19:20:30.001Z']",
                                document, XPathConstants.NODESET);
                
                System.out.println("nodeSet.getLength() = " + nodeSet.getLength());
                
        } catch (Exception e) {
                assertTrue(false);
                e.printStackTrace();
        }
        
        System.out.println("test_xpath_DOM end");
}

public void test_xpath_JAXB() {
        
        System.out.println("test_xpath_JAXB start");
        
        try {
                
                JAXBContext jaxbContext = JAXBContext.newInstance("snaplog.types");
                SnaplogMeta sm = (SnaplogMeta)
jaxbContext.createUnmarshaller().unmarshal(new
File(System.getProperty("tmp.path") + File.separator +
"../resources_1/snaplog-meta.xml"));
        
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
        
                Document document = db.newDocument();
                jaxbContext.createMarshaller().marshal(sm, document);
                
                OutputFormat format = new OutputFormat(document);
                XMLSerializer output = new XMLSerializer(System.out, format);
                output.serialize(document);
                
                System.out.println();
                
                XPath xpath = XPathFactory.newInstance().newXPath();
                
                NodeList nodeSet = (NodeList)
xpath.evaluate("/snaplog-meta/database-part[@from-ge-date =
'1997-07-16T19:20:30.001Z']",
                                document, XPathConstants.NODESET);
                
                System.out.println("nodeSet.getLength() = " + nodeSet.getLength());
                
        } catch (Exception e) {
                assertTrue(false);
                e.printStackTrace();
        }
        
        System.out.println("test_xpath_JAXB end");
}

returns the following in stdout:

test_xpath_DOM start
<?xml version="1.0" encoding="UTF-8"?>
<snaplog-meta xmlns="types.snaplog"><database-part
from-ge-date="1997-07-16T19:20:30.001Z"
name="db1.xml">foo</database-part></snaplog-meta>
nodeSet.getLength() = 1
test_xpath_DOM end
test_xpath_JAXB start
<?xml version="1.0" encoding="UTF-8"?>
<snaplog-meta xmlns="types.snaplog"><database-part
from-ge-date="1997-07-16T19:20:30.001Z"
name="db1.xml">foo</database-part></snaplog-meta>
nodeSet.getLength() = 0
test_xpath_JAXB end

thanks!

r.S.