users@jaxb.java.net

Re: Newbie Questions

From: Han Ming Ong <hanming_at_mac.com>
Date: Wed, 05 Mar 2003 17:10:34 -0800

Oh, I thought you were still at the pretty-print problem...sorry.

As for the namespace problem, one idea could be to have a simple
identity XSLT script that excludes the prefix:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ns1="http://foo.bar"
     exclude-result-prefixes="ns1"
     version="1.0">

....
</xsl:stylesheet>

Other people may have other ideas (for example, solving it at the
source, during unmarshalling).

Han Ming

On Wednesday, March 5, 2003, at 04:54 PM, Malachi de AElfweald wrote:

> 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