This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Remove Indentation during Transformation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Remove Indentation during Transformation" Watch "Remove Indentation during Transformation" New topic
Author

Remove Indentation during Transformation

Sanjit Singh
Greenhorn

Joined: Nov 12, 2001
Posts: 27
Hi Everyone,
Below you will find a method that takes a Document object as a parameter and returns a byte array.
**************************************************************
private byte[] xmlToBytes(Document document) {
try {
TransformerFactory tfactory = TransformerFactory.newInstance();
Source source = new DOMSource(document);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Result result = new StreamResult(bout);
Transformer transformer = tfactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "no");
transformer.transform(source, result);
return bout.toByteArray();
}catch(Exception e) {
return null;
}
}
**************************************************************
You might have noticed that I set OutputKeys.INDENT to "no" during transformation, but this doesn�t seem to have an effect. I would like to get a byte array containing xml, but without any carriage returns or whitespaces (simply the whole xml without any gaps in between).
Can somebody please help me out? Is there a way to convert a dom to a byte array without carriage returns/whitespaces/blankspaces?
Thanks in advance and looking forward to some replies!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove Indentation during Transformation
 
Similar Threads
how to update the attribute value
Returning a byte-array after performing an XSLT
unable to remove spaces in xml
Handling special chars to avoid UTFDataFormatException
xml output formatting