Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4037250
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:19:27+00:00 2026-05-20T12:19:27+00:00

be gentle. I’m trying to use javax.xml.transform.Transformer to format some xml string to be

  • 0

be gentle.

I’m trying to use javax.xml.transform.Transformer to format some xml string to be indented / spaceless between the tags. If there are no spaces between the tags, it works ok. If there are it acts weird. I’ll post an example. I tried to follow up on the following topic : http://forums.sun.com/thread.jspa?messageID=2054303#2699961. No success.

Code to follow :

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   factory.setIgnoringElementContentWhitespace(true);
   DocumentBuilder builder = factory.newDocumentBuilder();
   DOMImplementation domImpl = builder.getDOMImplementation();
   DOMImplementationLS ls = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
   LSInput in = ls.createLSInput();
   in.setByteStream(new ByteArrayInputStream(input.getBytes()));
   LSParser parser = ls.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS,
     "http://www.w3.org/2001/XMLSchema");
   Document xmlInput = parser.parse(in);

   StringWriter stringWriter = new StringWriter();
   StreamResult xmlOutput = new StreamResult(stringWriter);
   TransformerFactory f = TransformerFactory.newInstance();
   f.setAttribute("indent-number", 2);

   Transformer transformer = f.newTransformer();
   transformer.setOutputProperty(OutputKeys.INDENT, "yes");
   transformer.setOutputProperty(OutputKeys.METHOD, "xml");
   transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
   transformer.transform(new DOMSource(xmlInput), xmlOutput);

If there’s no interruption between tags

input : <tag><nested>    hello   </nested></tag>
output : 
<tag>
  <nested>    hello   </nested>
</tag>

If there is :

input : <tag>  <nested>    hello   </nested></tag>
output : 
<tag>  <nested>    hello   </nested>
</tag>

JVM 1.6.

Is something obvious wrong here ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:19:28+00:00Added an answer on May 20, 2026 at 12:19 pm

    This must be an issue with the transformer implementation. I’ve created a small test class that reads a String with no whitespace or line breaks as XML and creates a transformer from an XSLT stylesheet (also from a String). The stylesheet specifies that indentation must happen. This is basically another way of achieving what you’ve done with transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    Here it is:

    package transformation;
    
    import java.io.StringReader;
    
    import javax.xml.transform.Result;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;
    
    public class TransformerTest {
    
        public static void main(String[] args) throws Exception {
            
            final String xmlSample = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><tag><nested>hello</nested></tag>";
            final String stylesheet = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"xml\" version=\"1.0\" indent=\"yes\"/><xsl:template match=\"node()|@*\"><xsl:copy><xsl:apply-templates select=\"node()|@*\"/></xsl:copy></xsl:template></xsl:stylesheet>";
            
            final TransformerFactory factory = TransformerFactory.newInstance();
            
            final Source xslSource = new StreamSource(new StringReader(stylesheet));
            final Transformer transformer = factory.newTransformer(xslSource);
            
            final Source source = new StreamSource(new StringReader(xmlSample));
            final Result result = new StreamResult(System.out);
            
            transformer.transform(source, result);
    
        }
    
    }
    

    Now the curious thing is, results vary based on the transformer I use. If I don’t place any TransformerFactory implementation on the classpath (using the default implementation in the JRE libs), the result is this:

    <?xml version="1.0" encoding="UTF-8"?>
    <tag>
    <nested>hello</nested>
    </tag>
    

    Not correct, since the tag isn’t indented.

    Then, by adding a recent Xalan implementation on the classpath (xalan.jar and serializer.jar, still using JRE default parsers/DOM builders), I get this:

    <?xml version="1.0" encoding="UTF-8"?><tag>
    <nested>hello</nested>
    </tag>
    

    Still not correct, the first tag is on the same line as the XML declaration AND isn’t indented.

    To be honest, this quite shocked me. I’d understand if whitespace between tags or around text nodes would influence the indentation, as the transformer might assume some of it is non-ignorable. But to see a straightforward XML like that mangled is plain weird. I thought perhaps using the console output might have something to do with it, so I tried streaming to a file. Same result.

    Kind of weird how long-standing transformer implementations still have such behaviour. But not nearly as bad as when I noticed using a Validator of a Schema resulted in attributes being dropped from the "enhanced" XML output.

    So it would seem there’s not much to be done about this, apart from trying to find other processors and see if they’re having the same problem. Maybe Saxon is worth a shot. This bug report is interesting too (it is for Java 1.5, however):
    https://bugs.java.com/bugdatabase/view_bug?bug_id=6296446

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: This is my first time writing unit tests...be gentle! :) I am trying
I'm relatively new to programming in general so be gentle =| I'm trying to
this is my first question, so be gentle :) I'm trying to clear the
Please be gentle, I'm a newb to this IoC/MVC thing but I am trying.
I'm reading through A Gentle Introduction to Haskell, and early on it uses this
Be gentle, as my macrofoo is weak. What I'd like to do is something
I'm reading A Gentle Introduction to Haskell (which is not so gentle) and it
Please be gentle, as I'm still new to web programming and -very- new to
On page 224 of Common Lisp: A Gentle Introduction to Symbolic Computation this example
Totally new to Drupal so be gentle please! Let's say I have a number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.