Read a Simple XML File

Updated: Jul 11, 2023

This example shows you how to read an XML file in Java using the SimpleXmlReader class. XML is a common data format for exchanging information between different systems. DataPipeline can be used to consume XML from APIs, files, and data feeds, allowing users to integrate external data into their applications and extract the necessary information for further processing or analysis.

For demo purposes, this example reads an XML file and writes its contents to the console. However, the input XML data can also be written to other output sources.

There are other examples that show how to write to an XML file or write to an XML file programmatically or write to an XML file using freemarker templates.

 

Java code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import java.io.File;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.xml.SimpleXmlReader;

public class ReadSimpleXmlFile {

    public static void main(String[] args) {
        DataReader reader = new SimpleXmlReader(new File("example/data/output/simple-xml-to-file.xml"));
        
        Job.run(reader, new StreamWriter(System.out));
    }
}

 

Code Walkthrough

  1. SimpleXmlReader is created corresponding to the input file i.e. simple-xml-to-file.xml.
  2. Data is transferred from the XML file to the StreamWriter via Job.run().
  3. StreamWriter is used to write the output to the console.

 

Output

Records of the input file simple-xml-to-file.xml are printed on the console.

Mobile Analytics