How to Write formatted xml in Java

To write a formatted xml in java you have to set some properties of Transformer

 TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");

Above settings instruct Transformer to spit a formatted xml. See complete example below

package com.bharat.saxparser;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class WriteFormattedXmlExample
{

  public static void main(String[] args) throws Exception
  {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    // root element
    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("company");
    doc.appendChild(rootElement);

    // staff element
    Element employee = doc.createElement("employee");
    rootElement.appendChild(employee);

    // set attribute to staff element
    Attr attr = doc.createAttribute("id");
    attr.setValue("1001");
    employee.setAttributeNode(attr);

    // shorten way
    // staff.setAttribute("id", "1");

    // firstname elements
    Element firstname = doc.createElement("firstname");
    firstname.appendChild(doc.createTextNode("Bharat"));
    employee.appendChild(firstname);

    // lastname elements
    Element lastname = doc.createElement("lastname");
    lastname.appendChild(doc.createTextNode("Sharma"));
    employee.appendChild(lastname);

    // nickname elements
    Element nickname = doc.createElement("nickname");
    nickname.appendChild(doc.createTextNode("b"));
    employee.appendChild(nickname);

    // salary elements
    Element salary = doc.createElement("salary");
    salary.appendChild(doc.createTextNode("2000000"));
    employee.appendChild(salary);

    // write xml to file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");

    DOMSource source = new DOMSource(doc);

    StreamResult result = new StreamResult(new File("file.xml"));

    // Show output on console during development
    //StreamResult result = new StreamResult(System.out);

    transformer.transform(source, result);

    System.out.println("Xml File saved!");

  }
}

Output:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<company>
  <employee id="1001">
    <firstname>Bharat</firstname>
    <lastname>Sharma</lastname>
    <nickname>b</nickname>
    <salary>2000000</salary>
  </employee>
</company>

Advertisements

, , , ,

  1. #1 by parag shah on September 5, 2012 - 8:03 am

    Thanks Bharat! your example helped.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: