Xml indent in java

Java DocumentBuilder — wrong indentation in XML file

I try to write a simple XML file in Java using the DocumentBuilder. I expected the XML file to look like this:

Why the third element does not have the same indentation as the other two elements? Note: I read the XML file again to simulate a method in a project, where I read an XML file, add one element and save it to the XML file. Here is my code:

import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.File; import java.io.IOException; public class main < private static String FILEPATH = "/tmp/xmltest.xml"; private static DocumentBuilderFactory docFactory; private static DocumentBuilder docBuilder; private static TransformerFactory transformerFactory; private static Transformer transformer; public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, TransformerConfigurationException, TransformerException< docFactory = DocumentBuilderFactory.newInstance(); docBuilder = docFactory.newDocumentBuilder(); transformerFactory= TransformerFactory.newInstance(); transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("indent-amount", "1"); // Creating the XML file structure Document document = docBuilder.newDocument(); Element rootElement = document.createElement("outer"); document.appendChild(rootElement); Element inner = document.createElement("inner"); rootElement.appendChild(inner); // Write XML file write(document); // Read XML file document = docBuilder.parse(FILEPATH); Element root = document.getDocumentElement(); Element innerElement = (Element)root.getElementsByTagName("inner").item(0); // Add element Element e = document.createElement("element"); e.setAttribute("name", "WEB"); innerElement.appendChild(e); // Add element e = document.createElement("element"); e.setAttribute("name", "WEB"); innerElement.appendChild(e); // Write XML file write(document); // Read XML fil document = docBuilder.parse(FILEPATH); root = document.getDocumentElement(); innerElement = (Element)root.getElementsByTagName("inner").item(0); // Add element e = document.createElement("element"); e.setAttribute("name", "WEB"); innerElement.appendChild(e); // Write XML file write(document); > private static void write(Document document) throws TransformerException < DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(FILEPATH)); transformer.transform(source, result); >> 

Источник

Читайте также:  Get hash from url javascript

Java: How to Indent XML Generated by Transformer

I’m using Java’s built in XML transformer to take a DOM document and print out the resulting XML. The problem is that it isn’t indenting the text at all despite having set the parameter «indent» explicitly. sample code

7 Answers 7

You need to enable ‘INDENT’ and set the indent amount for the transformer:

t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("indent-amount", "2"); 

(Many thanks to all members especially @marc-novakowski, @james-murty and @saad):

Seems silly to me that the default indentation is 0, but in addition to INDENT=yes I also had to add this: t.setOutputProperty(«indent-amount», «2»);

If there are inner nodes that are multiple lines, can you indent the inner portion as well? Just using this doesn’t indent the inner nodes.

I have a similar problem which @eipark might be referring. I am converting a plain string of XML to a Node and then using transformer to indent it. My plain string contains whitespaces and the indentation does not seem to work (given the above suggestions). I will try to remove the whitespaces before converting to Node, perhaps that would work.

@lapo if your provider is xalan (which it probably is if this works), then it’s available as org.apache.xml.serializer.OutputPropertiesFactory.S_KEY_INDENT_AMOUNT

Neither of the suggested solutions worked for me. So I kept on searching for an alternative solution, which ended up being a mixture of the two before mentioned and a third step.

  1. set the indent-number into the transformerfactory
  2. enable the indent in the transformer
  3. wrap the otuputstream with a writer (or bufferedwriter)
//(1) TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", new Integer(2)); //(2) Transformer t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); //(3) t.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "utf-8")); 

You must do (3) to workaround a «buggy» behavior of the xml handling code.

Читайте также:  Hide borders in html table

(If I have cited my source incorrectly please let me know)

Источник

How to Indent XML String in Java (Pretty)

We often need to format a text block in xml to better visualize its content, after all we are human, aren’t we? In this post, we demonstrate a block of source code in java that does this job for us … I particularly use it a lot to inspect the request and response of a WebService in Soap … erghhh Soap. I know … But sometimes it is necessary … Have fun

This is a cool way to prettify your XML (String format) in Java Language:

import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class FormatXML < public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException < System.out.println(format("YOUR_XML_HERE", true)); >public static String format(String xml, Boolean ommitXmlDeclaration) throws IOException, SAXException, ParserConfigurationException < DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = db.parse(new InputSource(new StringReader(xml))); OutputFormat format = new OutputFormat(doc); format.setIndenting(true); format.setIndent(2); format.setOmitXMLDeclaration(ommitXmlDeclaration); format.setLineWidth(Integer.MAX_VALUE); Writer outxml = new StringWriter(); XMLSerializer serializer = new XMLSerializer(outxml, format); serializer.serialize(doc); return outxml.toString(); >>

Источник

Get Java to use tabs instead of spaces for indenting

I’m creating an XML document. I got it to indent using TransformerFactory.setAttribute(«indent-number», new Integer(2)); Transformer.setOutputProperty(OutputKeys.INDENT, «yes»); Is it possible to get Java to use tabs instead of spaces for indenting? And how?

transformer.setOutputProperty(«indent-amount», «4»); did the trick for me. It’s probably no guarantee, and in fact it seems to be highly dependent upon the version of java you are using. Personally tabs vs. spaces means nothing when it’s just meant to be a data file.

Читайте также:  Python запуск программы от имени администратора

3 Answers 3

No, not in general. The XSLT specification does not allow for specifying WHAT whitespace to use when indenting.

It might, however, be a XSLT-processor specific item to configure. Check the documentation for the one you are using.

If you REALLY want this, then you can use an afterburner XSLT-script on the output which does whatever you want to do on text()-nodes.

Yes, tabs are considered to be evil by a few. However, if you want to use TransformFactory and want to change the indenting behavior to use tabs instead of spaces, you need to provide your own implementation of ContentHandler . Then pass your implementation of ContentHandler into a new SAXResult — pass that as the «result» to the Transformer.transform(. ) . Lot of hoops to jump through. Another consideration may be to use a smart XSLT over your output.

You can get the result and process it, replacing calling str.replace(«\n «, «\n\t») (or whatever number of spaces you want).

But I’d recommend against that — tabs aren’t platform independent.

@Bozho, it may be dangerous to do a str.replace call as it might end up replacing some attribute values etc to tab from space; very undesirable isn’t it?

Источник

Оцените статью