Write to XML Stream (Simple)

Updated: Feb 21, 2022

This example shows you how to create an XML file in Java using the SimpleXmlWriter class.

The demo code creates some test data in memory and creates a new XML file using this data. However, other input sources like CSV, Excel, etc may also be used.

This example can easily be modified to show how to read an XML file.

Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import java.io.FileWriter;

import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.core.RecordList;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.memory.MemoryReader;
import com.northconcepts.datapipeline.xml.SimpleXmlWriter;

public class WriteToXmlStream {
	
	
/* Produces the following XML (line breaks added for clarity)
  


    
        John Wayne
        Marion Robert Morrison
        male
        Winterset
        156.35
    
    
        Spiderman
        Peter Parker
        male
        New York
        -0.96
    


*/

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

        Record record1 = new Record();
        record1.getField("stageName", true).setValue("John Wayne");
        record1.getField("realName", true).setValue("Marion Robert Morrison");
        record1.getField("gender", true).setValue("male");
        record1.getField("city", true).setValue("Winterset");
        record1.getField("balance", true).setValue(156.35);

        Record record2 = new Record();
        record2.getField("stageName", true).setValue("Spiderman");
        record2.getField("realName", true).setValue("Peter Parker");
        record2.getField("gender", true).setValue("male");
        record2.getField("city", true).setValue("New York");
        record2.getField("balance", true).setValue(-0.96);
               
        MemoryReader reader = new MemoryReader(new RecordList(record1, record2));
        
        SimpleXmlWriter writer = new SimpleXmlWriter(new FileWriter("example/data/output/credit-balance-05.xml"));
        
        Job.run(reader, writer);
    }

}

Code Walkthrough

  1. Two Record objects are created in memory with the fields stageName,realName,gender,city and balance set to some test values.
  2. A RecordList is created with these Record objects and a MemoryReader is created to read this RecordList..
  3. A SimpleXmlWriter is created using the file path of the output XML file credit-balance-05.xml.
  4. Data is transferred from memory to the output XML file via JobTemplate.DEFAULT.transfer method.

SimpleXmlWriter

A SimpleXmlWriter is an output writer that can be used to write to an XML stream. It is a sub-class of TextWriter.html and overrides the open and close among other methods. It encapsulates all the details of writing data to an XML file, so the user need not write this code.
XML files may also be created programmatically (refer write to an XML file programmatically) or using freemarker templates (refer write to an XML file using freemarker templates). These approaches should be used when one needs more control over the XML creation process.

Output XML file

<?xml version="1.0" ?>
<records>
    <record>
        <field name="stageName">John Wayne</field>
        <field name="realName">Marion Robert Morrison</field>
        <field name="gender">male</field>
        <field name="city">Winterset</field>
        <field name="balance">156.35</field>
    </record>
    <record>
        <field name="stageName">Spiderman</field>
        <field name="realName">Peter Parker</field>
        <field name="gender">male</field>
        <field name="city">New York</field>
        <field name="balance">-0.96</field>
    </record>
</records>
Mobile Analytics