Write a Simple XML File
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")) .setPretty(true); Job.run(reader, writer); } }
Code Walkthrough
- First, Record is created to to persist data in a key-value field.
- 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
andlanguage
and values are added to each fields asrecord1.setField("country", "Philippines")
. - MemoryReader is created to obtain records from an in-memory RecordList.
SimpleXmlWriter
is created to write to an outputsimple-xml-to-file.xml
file.setPretty(true)
method enables line breaks and indentations to be automatically added to the output XML file(disabled by default).- Data are transferred from ParquetDataReader to the console via Job.run() method. See how to compile and run data pipeline jobs.
SimpleXmlWriter
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
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.
RecordList
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.
MemoryReader
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'?> <records> <record> <field name="country">Philippines</field> <field name="capital">Manila</field> <field name="language">Filipino</field> </record> <record> <field name="country">Japan</field> <field name="capital">Tokyo</field> <field name="language">Japanese</field> </record> </records>
The output will be written to the XML file and stored in the specified location i.e example/data/output
.