users@jaxb.java.net

Binder - maintaining the association

From: Jason Harrop <jharrop_at_gmail.com>
Date: Mon, 12 Jul 2010 18:42:54 +1000

Hi

I'd like to maintain an up to date association between my JAXB
elements and XML nodes, so I can use XPath to return relevant JAXB
elements.

I have no problem establishing the initial association, if I start from the XML:

        org.w3c.dom.Document doc = dbf.newDocumentBuilder().parse(is);
        binder = jaxbContext.createBinder();
        jaxbElement = binder.unmarshal( doc );

And I can use this to evaluation xpaths on node:

        Node node = binder.getXMLNode(jaxbElement );
        NodeList nodes =
                    (NodeList) xPath.evaluate(myxpath,
                        node, XPathConstants.NODESET);

and these nodes are associated with my JAXB objects.

The problem is that I can only call updateXML() once, as per
https://jaxb.dev.java.net/issues/show_bug.cgi?id=459
(Binder.updateXML() fails if called twice)

To work around this, I thought I'd establish the association starting from JAXB:

        javax.xml.parsers.DocumentBuilderFactory dbf
                = DocumentBuilderFactory.newInstance();
        org.w3c.dom.Document doc = dbf.newDocumentBuilder().newDocument();
        binder.marshal(jaxbElement, doc);

This does marshall to the doc (I can see its contents), but doesn't
establish the association.
That is,

        NodeList nodes =
                    (NodeList) xPath.evaluate(myxpath,
                        node, XPathConstants.NODESET);

returns results, but for each result, binder.getJAXBNode(nodes.item(i)) is null.

On my reading of the javadoc, this is a bug. In any case, I'm stuck.

If I want to use xpath after modifying my jaxb objects, I know I could
marshall my object tree to xml, and then use binder.unmarshal (as
outlined above), but this effectively re-creates my jaxb objects,
which I don't want to do (since there might be user data structures
built around the existing jaxb objects).

Any ideas? I've tried both JAXB 2.1.x and 2.2.1.

thanks

Jason