• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Remove Indentation during Transformation

Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic