[jdom-interest] JDOM Documents from JAXBContext

Anderson, Paul D paul.d.anderson at boeing.com
Fri Jul 15 13:20:33 PDT 2005


The project I'm involved in uses JAXB 2.0 for marshalling/unmarshalling
of XML objects, but internally I'd like to use JDOM for manipulating the
XML.

I'm having trouble finding an efficient way to convert the JAXBContext
output to JDOM. The most straightforward solution I can come up with is
this: 

	// The object to be marshalled.
	Test obj = new Test();
	// this is boilerplate to create a Marshaller
	JAXBContext jc = JAXBContext.newInstance( Test.class );
	Marshaller m = jc.createMarshaller(); 
	// once I have the Marshaller I can marshal to Files, Writers,
OutputStreams, etc.
	// The most straightforward path to JDOM I can find is to
marshal to a org.w3c.dom.Document...
	DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
	dbf.setNamespaceAware(true);
	DocumentBuilder db = dbf.newDocumentBuilder();
	Document doc = db.newDocument(); 
	m.marshal( obj, doc ); 
	// ... and then convert the org.w3c.dom.Document to a
org.jdom.Document
	Org.jdom.DOMBuilder jdb = new org.jdom.DOMBuilder();
	org.jdom.Document jdoc = jdb.build(doc);

This works okay, but it seems inefficient to create the
org.w3c.dom.Document. 

The marshaller will also marshal to a SAX ContentHandler or a
javax.transform.sax.SAXResult but I can't find a way to hook these into
the JDOM SAXBuilder.

Does anyone know of a better way to do this?

Paul D. Anderson
Software Engineer
Boeing Phantom Works
206-655-4716
paul.d.anderson at boeing.com

"Notions and scruples [are] like spilt needles, making one afraid of
treading, or sitting down, or even eating." George Eliot, Middlemarch




More information about the jdom-interest mailing list