tags:

views:

81

answers:

1

Hello,

I have an XML file which I'd like to parse into a non-XML (text) file based on a XLST file. The code in both seem correct, and it works when testing manually, but I'm having a problem doing this programatically.

I'm using JDOM's XSLTransformer class to apply the XSLT to the XML and it returns it in the format of a JDOM Document. The problem here is that I can't seem to access anything in the Document as it is not a proper XML file and I get a "java.lang.IllegalStateException: Root element not set" error.

Is there a better way within Java to obtain a non-XML file as a result of XSLT?

+2  A: 

JDOM XSLTTransformer is a convenience wrapper around javax.xml.transform.Transformer for JDOM input and output.

A JDOM input is easily transformed to text output.

Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(stylesheet));
JDOMSource in = new JDOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult out = new StreamResult(writer);
transformer.transform(in, out);
return writer.toString();
Lachlan Roche
Brilliant, worked perfectly.
Neil McF