I'm beautifying/indenting some XML in Java:
<div xml:space="default"><h1 xml:space="default">Indenting mixed content in Java</h1><p xml:space="preserve">Why does indenting mixed content (like this paragraph) add whitespace around <a href="http://www.stackoverflow.com" xml:space="preserve"><strong>this strong element</strong></a>?</p></div>
When I beautify the XML, I don't want whitespace added to the contents of the <a>
element, so I've specified xml:space="preserve"
expecting the transformer to preserve the white space therein.
However when I transform the XML, I get this:
<div>
<h1 xml:space="default">Indenting mixed content in Java</h1>
<p>Why does indenting mixed content (like this paragraph) add whitespace around <a href="http://www.stackoverflow.com">
<strong xml:space="preserve">this strong element</strong>
</a>?</p>
</div>
... with extra whitespace between the <a>
and the <strong>
element. (Not only that, but the </a>
close tag awkwardly doesn't line up with its open tag.)
How can I prevent the prettifier from adding that white space? Am I doing something wrong? Here's the Java code I'm using:
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import java.io.ByteArrayInputStream;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import java.io.StringWriter;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.stream.StreamResult;
public class XmlExample {
public static void main(String[] argv) {
Document xmlDoc = parseXml("<div xml:space=\"default\">" +
"<h1 xml:space=\"default\">Indenting mixed content in Java</h1>" +
"<p xml:space=\"preserve\">Why does indenting mixed content (like this paragraph) add whitespace around " +
"<a href=\"http://www.stackoverflow.com\" xml:space=\"preserve\"><strong>this strong element</strong></a>?" +
"</p>" +
"</div>");
String xmlString = xmlToString(xmlDoc.getDocumentElement());
System.out.println(xmlString);
}
public static Document parseXml(String xml) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
return doc;
}
catch(Exception e) {
throw new RuntimeException(e);
}
}
public static String xmlToString(Element el) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
DOMSource source = new DOMSource(el);
transformer.transform(source, new StreamResult(writer));
return writer.getBuffer().toString().trim();
}
catch(Exception e) {
throw new RuntimeException(e);
}
}
}