users@jaxb.java.net

Re: Newbie Questions

From: Malachi de AElfweald <malachi_at_EOTI.ORG>
Date: Wed, 05 Mar 2003 17:54:04 -0700

On Wed, 5 Mar 2003 16:20:14 -0800, Han Ming Ong <hanming_at_mac.com> wrote:
>> Is there any way, currently, without using 3rd party stuff, to
>> Marshall it correctly to a file or stream or whatever? Perhaps
>> outputting it via JAXP or something....?
>I use javax.xml.bind.util.JAXBResult and then feed that into an XSLT
>transformer to get nicely indented output.
>
>Here's what you can find from the API doc:
>
> JAXBResult result = new JAXBResult(
> JAXBContext.newInstance("org.acme.foo") );
>
> // set up XSLT transformation
> TransformerFactory tf = TransformerFactory.newInstance();
> Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
>
> // run transformation
> t.transform(new StreamSource("document.xml"),result);
>
> // obtain the unmarshalled content tree
> Object o = result.getReult();

Hmmm. Are you saying I would have to provide an XSL and actually do a transform in order to get the output correctly?

I did try outputting to JDOMResult:

 JDOMResult result = new JDOMResult();
 m.marshal((Object)objex, (javax.xml.transform.sax.SAXResult)result);
 Document doc = result.getDocument();
 org.jdom.Element rootElem = doc.getRootElement();
 Namespace xsins = Namespace.getNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
 rootElem.addNamespaceDeclaration(xsins);
 rootElem.setAttribute("schemaLocation", "http://www.temporal-wave.com/spec jbase.xsd", xsins);
 XMLOutputter xout = new XMLOutputter(" ", true);
 xout.output(doc, System.out);

but, that still gave me the xmlns:ns1.... It did pretty format (the empty-tag compression and the newline/indentions)... but it still didn't let me specify the namespace. And JDOM doesn't allow you to change the namespace... And it still required a 3rd party...

I am looking over Marshaller, and JAXBResult -- but don't see a way to output it without the overhead of transformation....

Malachi