JAXP: serializing XML with identity transform, but no indent?

L

lard

I'm serializing an XML doc from a DOM representation, using the usual
identity/null transformation method. I'm setting the output encoding
and enabling indent (indent="yes") on the output; in the resulting XML,
the output encoding is coming out as desired, but there are no indents
- all lines of XML are flush left with no spaces in front.
In other words, I'm expecting to see:

<A>
<B>stuff</B>
<C>other stuff</C>
</A>

but instead just get the unindented text as follows:
<A>
<B>stuff</B>
<C>other stuff</C>
</A>


The method that does the serialization is as follows:

public static void writeXML(Node node, OutputStream out) {

TransformerFactory factory = TransformerFactory.newInstance();
Transformer identityTransformer = factory.newTransformer();


Properties outputProps = new Properties();
outputProps.setProperty("encoding", "ISO-8859-1");
outputProps.setProperty("indent", "yes");
identityTransformer.setOutputProperties(outputProps);

DOMSource domSource = new DOMSource(node);
StreamResult streamResult = new StreamResult(out);
identityTransformer.transform(domSource, streamResult);


(Sorry, I don't have a compilable self contained example yet.)

The code which calls the above method is as follows:

ByteArrayOutputStream byteArrayOutStream = new
ByteArrayOutputStream();
// domResult is a DOMResult instance
writeXML(domResult.getNode(), byteArrayOutStream);

// out is a ServletOutputStream
PrintWriter printWriter = new PrintWriter(out);

String resultXmlContent = byteArrayOutStream.toString();
printWriter.print(resultXmlContent);
printWriter.close();

// now we do other things with the resultXmlContent (log to
file)...


So does anyone have an ideas why does the indent="yes" directive appear
to be being ignored?

thanks
Alex
 
L

lard

Sorry, forgetting my manners... forgot to say I'm using JDK1.4.2_11 and
the inbuilt JAXP with default implementations for XSLT and XML
processing...
 
L

lard

lard said:
Sorry, forgetting my manners... forgot to say I'm using JDK1.4.2_11 and
the inbuilt JAXP with default implementations for XSLT and XML
processing...

Problem solved. I needed to set "indent-amount" too...

The following line did it:


identityTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
"2");


alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top