Write a Simple XML File

Updated: Jun 8, 2023

In this example you are going to see how to write records to an XML file using Data Pipeline's SimpleXmlWriter.

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

Java Code listing

package com.northconcepts.datapipeline.examples.cookbook;

import java.io.File;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
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 WriteSimpleXmlFile {

    public static void main(String[] args) {
        Record record1 = new Record();
        record1.setField("country", "Philippines");
        record1.setField("capital", "Manila");
        record1.setField("language", "Filipino");
        Record record2 = new Record();
        record2.setField("country", "Japan");
        record2.setField("capital", "Tokyo");
        record2.setField("language", "Japanese");
        DataReader reader = new MemoryReader(new RecordList(record1, record2));
        DataWriter writer = new SimpleXmlWriter(new File("example/data/output/simple-xml-to-file.xml"))
        Job.run(reader, writer);



Code Walkthrough

  1. First, Record is created to to persist data in a key-value field.
  2. Record.setField() method is used to add new fields with the specified field name and value. In this example there are three fields country, capital and language and values are added to each fields as record1.setField("country", "Philippines").
  3. MemoryReader is created to obtain records from an in-memory RecordList.
  4. SimpleXmlWriter is created to write to an output simple-xml-to-file.xml file.
  5. setPretty(true) method enables line breaks and indentations to be automatically added to the output XML file(disabled by default).
  6. Data are transferred from ParquetDataReader to the console via Job.run() method. See how to compile and run data pipeline jobs.


Writes XML in the following simple format. It extends TextWriter class and can be created with either File or Writer object. A method setPretty() in this class indicates if line breaks and indentations should be added to output (default false). Notice how the output XML file is nicely formatted with indentations and line breaks.


Record class holds persistent data in key-value fields as it flows through the pipeline. A method setField() in this class creates a new field as key-value pair by taking field name and a value for that field as a parameter.


As the name suggests it is used to store a list of Record objects in memory. It implements Iterable so you can perform operations similar to Java Collection to this object.


Obtains records from an in-memory RecordList. You can obtain records added to this object via getRecordList() method.

Output XML file

<?xml version='1.0' encoding='UTF-8'?>
    <field name="country">Philippines</field>
    <field name="capital">Manila</field>
    <field name="language">Filipino</field>
    <field name="country">Japan</field>
    <field name="capital">Tokyo</field>
    <field name="language">Japanese</field>

The output will be written to the XML file and stored in the specified location i.e example/data/output.

Mobile Analytics