Convert Java Object to XML using Java StAX api

The XMLStreamWriter class in the Java StAX API allows us to write XML documents.

This class is provided in javax.xml.stream package.
 
convert java object to xml
 
Here are the steps for converting JAVA objects to XML document:

– Create an XMLOutputFactory

– Create a FileOutputStream

– Create an XMLStreamWriter using the XMLOutputFactory and FileOutputStream

– Use the XMLStreamWriter’s methods to create the XML document and add elements to it from Java object.
 

Example code

 
We will write the data from following Device POJO objects into a XML

package com.topjavatutorial.stax;

public class Device {
  String name;
  String model;

  public Device(String name, String model) {
    this.name = name;
    this.model = model;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getModel() {
    return model;
  }

  public void setModel(String model) {
    this.model = model;
  }

}


 
Here is the code for writing devices data into device.xml file :
 
package com.topjavatutorial.stax;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

public class XMLWriter {

  public static void main(String[] args) {
    
    String file="C:\\stax\\Devices.xml";
    
    
    List<Device> deviceList = new ArrayList<Device>();
    deviceList.add(new Device("iPhone","6s"));
    deviceList.add(new Device("iPhone","7"));
    deviceList.add(new Device("iPad","2"));
    
    XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
    
    try(FileOutputStream fos = new FileOutputStream(file)){
      XMLStreamWriter writer = outputFactory.createXMLStreamWriter(fos);
      
      writer.writeStartDocument();
      writer.writeCharacters("\n");
      writer.writeStartElement("devices");
      writer.writeCharacters("\n");
      
      for(Device d : deviceList){
        writer.writeCharacters("\t");
        writer.writeStartElement("device");
        writer.writeCharacters("\n\t\t");
        writer.writeStartElement("name");
        writer.writeCharacters(d.getName());
        writer.writeEndElement();
        writer.writeCharacters("\n\t\t");
        writer.writeStartElement("model");
        writer.writeCharacters(d.getModel());
        writer.writeEndElement();
        writer.writeCharacters("\n\t");
        writer.writeEndElement();
        writer.writeCharacters("\n");
      }
      writer.writeEndElement();
      writer.writeEndDocument();
      writer.close();
      
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (XMLStreamException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}


 

Output

 
device.xml created in “C:\\stax\\Devices.xml” path has following output :
 

<?xml version="1.0" ?>
<devices>
  <device>
    <name>iPhone</name>
    <model>6s</model>
  </device>
  <device>
    <name>iPhone</name>
    <model>7</model>
  </device>
  <device>
    <name>iPad</name>
    <model>2</model>
  </device>
</devices>

 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

You may also like...

Leave a Reply.. code can be added in <code> </code> tags