XMLStreamWriter Java StAX

Filed Under: Java

In last post we learned how to use Java StAX Iterator API to write XML, here we will use StAX cursor API to write XML file.

XMLStreamWriter

Java StAX Cursor API is very straight forward in creating XML and outputting it. We need to create XMLStreamWriter object and write data into it. Useful methods are writeStartDocument(), writeStartElement(), writeCharacters(), writeEndElement() and writeEndDocument().

XMLStreamWriter Example

Here we will create a HashMap and then write it to XML file.


package com.journaldev.xml.stax;

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

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

public class StaxXMLStreamWriter {

    public static void main(String[] args) {
        String fileName = "/Users/pankaj/tmp/employee.xml";
        String rootElement = "Employee";
        StaxXMLStreamWriter xmlStreamWriter = new StaxXMLStreamWriter();
        Map<String,String> elementsMap = new HashMap<String, String>();
        elementsMap.put("id", "1");
        elementsMap.put("name", "Pankaj");
        elementsMap.put("age", "29");
        elementsMap.put("role", "Java Developer");
        elementsMap.put("gender", "Male");
        xmlStreamWriter.writeXML(fileName, rootElement, elementsMap);
    }

    private void writeXML(String fileName, String rootElement, Map<String, String> elementsMap) {
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        try{
            XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(new FileOutputStream(fileName), "UTF-8");
            //start writing xml file
            xmlStreamWriter.writeStartDocument("UTF-8", "1.0");
            xmlStreamWriter.writeCharacters("\n");
            xmlStreamWriter.writeStartElement(rootElement);
            
            //write id as attribute
            xmlStreamWriter.writeAttribute("id", elementsMap.get("id"));
            
            //write other elements
            xmlStreamWriter.writeCharacters("\n\t");
            xmlStreamWriter.writeStartElement("name");
            xmlStreamWriter.writeCharacters("\n\t\t"+elementsMap.get("name"));
            xmlStreamWriter.writeCharacters("\n\t");
            xmlStreamWriter.writeEndElement();
            
            xmlStreamWriter.writeCharacters("\n\t");
            xmlStreamWriter.writeStartElement("age");
            xmlStreamWriter.writeCharacters("\n\t\t"+elementsMap.get("age"));
            xmlStreamWriter.writeCharacters("\n\t");
            xmlStreamWriter.writeEndElement();
            
            xmlStreamWriter.writeCharacters("\n\t");
            xmlStreamWriter.writeStartElement("gender");
            xmlStreamWriter.writeCharacters("\n\t\t"+elementsMap.get("gender"));
            xmlStreamWriter.writeCharacters("\n\t");
            xmlStreamWriter.writeEndElement();
            
            xmlStreamWriter.writeCharacters("\n\t");
            xmlStreamWriter.writeStartElement("role");
            xmlStreamWriter.writeCharacters("\n\t\t"+elementsMap.get("role"));
            xmlStreamWriter.writeCharacters("\n\t");
            xmlStreamWriter.writeEndElement();
            //write end tag of Employee element
            xmlStreamWriter.writeCharacters("\n");
            xmlStreamWriter.writeEndElement();
            
            //write end document
            xmlStreamWriter.writeEndDocument();
            
            //flush data to file and close writer
            xmlStreamWriter.flush();
            xmlStreamWriter.close();
            
        }catch(XMLStreamException | FileNotFoundException e){
            e.printStackTrace();
        }
    }

}

When we execute above XMLStreamWriter example program, we get following XML file.


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

Notice the use of writeCharacters() for pretty print XML else it will be in compact format.

Comments

  1. X++ Developers says:

    If some one wants expert view on the topic of running a blog afterward i propose him/her to pay a quick visit this weblog, Keep up the good work.

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