How to write XML file in Java using Java StAX Iterator API

Filed Under: Java

Java Streaming API for XML or Java StAX API was introduced in Java 6 and considered superior to DOM and SAX parsers. As explained in earlier post about Java StAX API, it consists of cursor-based API and iterator based API.

Here we will see how we can write XML file in java using StAX Iterator based API (XMLEventWriter).


package com.journaldev.xml;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class StaxXMLWriter {

    public static void main(String[] args) {
        String fileName = "/Users/pankaj/employee.xml";
        String rootElement = "Employee";
        StaxXMLWriter xmlWriter = new StaxXMLWriter();
        Map<String,String> elementsMap = new HashMap<String, String>();
        elementsMap.put("name", "Pankaj");
        elementsMap.put("age", "29");
        elementsMap.put("role", "Java Developer");
        elementsMap.put("gender", "Male");
        
        xmlWriter.writeXML(fileName, rootElement, elementsMap);
    }
    
    public void writeXML(String fileName, String rootElement, Map<String, String> elementsMap){
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        try {
            XMLEventWriter xmlEventWriter = xmlOutputFactory
                    .createXMLEventWriter(new FileOutputStream(fileName), "UTF-8");
            //For Debugging - below code to print XML to Console
            //XMLEventWriter xmlEventWriter = xmlOutputFactory.createXMLEventWriter(System.out);
            XMLEventFactory eventFactory = XMLEventFactory.newInstance();
            XMLEvent end = eventFactory.createDTD("\n");
            StartDocument startDocument = eventFactory.createStartDocument();
            xmlEventWriter.add(startDocument);
            xmlEventWriter.add(end);
            StartElement configStartElement = eventFactory.createStartElement("",
                "", rootElement);
            xmlEventWriter.add(configStartElement);
            xmlEventWriter.add(end);
            // Write the element nodes
            Set<String> elementNodes = elementsMap.keySet();
            for(String key : elementNodes){
                createNode(xmlEventWriter, key, elementsMap.get(key));
            }
            
            xmlEventWriter.add(eventFactory.createEndElement("", "", rootElement));
            xmlEventWriter.add(end);
            xmlEventWriter.add(eventFactory.createEndDocument());
            xmlEventWriter.close();

        } catch (FileNotFoundException | XMLStreamException e) {
            e.printStackTrace();
        }
    }
    
    private static void createNode(XMLEventWriter eventWriter, String element,
            String value) throws XMLStreamException {
          XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();
          XMLEvent end = xmlEventFactory.createDTD("\n");
          XMLEvent tab = xmlEventFactory.createDTD("\t");
          //Create Start node
          StartElement sElement = xmlEventFactory.createStartElement("", "", element);
          eventWriter.add(tab);
          eventWriter.add(sElement);
          //Create Content
          Characters characters = xmlEventFactory.createCharacters(value);
          eventWriter.add(characters);
          // Create End node
          EndElement eElement = xmlEventFactory.createEndElement("", "", element);
          eventWriter.add(eElement);
          eventWriter.add(end);

        }

}

Above program creates below XML file:

employee.xml


<?xml version="1.0" encoding="UTF-8"?>
<Employee>
	<age>29</age>
	<name>Pankaj</name>
	<gender>Male</gender>
	<role>Java Developer</role>
</Employee>

I have used \n and \t elements to create the formatted XML but to save memory and reduce processing time, you can remove them from the implementation.

Also for debugging purpose, you can change the output stream to System.out to print the XML file to console.

Comments

  1. Harish Vembu says:

    Hello Pankaj,

    Thanks for your simple article to show the use of STAX API. Couls you also provide us with a multi node XML message. This would be more of a real world example.

    Thanks

  2. Abhijit says:

    Ya for Java its a nice thing……..
    But how we can use java Stax API in Android….

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages