You are on page 1of 6

05/10/2017 How to convert XML to Excel - Data Pipeline

How to convert XML to Excel


(/#facebook) (/#twitter) (/#google_plus) (/#linkedin) (/#pinterest) (/#pocket)
Share
(https://www.addtoany.com/share#url=https%3A%2F%2Fnorthconcepts.com%2Fblog%2F2015%2F06%2F22%2Fconvert-
xml-to-excel%2F&title=How%20to%20convert%20XML%20to%20Excel)

Data Pipeline makes it easy to read, transform, and write XML and Excel les. This post demonstrates how to load data from an on-
disk XML le, apply transformations on-the- y, and save the result to an Excel le.

I recommend you to take a look at the How to Read a JSON Stream (https://northconcepts.com/docs/examples/read-a-json-stream/)
post if you need to read data directly from a URL and skip the disk altogether. If you prefer to write your data to multiple Excel sheets,
you can to read the post How to create multiple sheets in a single Excel le (https://northconcepts.com/blog/2015/05/30/how-to-
create-multiple-sheets-in-a-single-excel- le/#more-817).

Download the Data Pipeline framework (https://northconcepts.com/pricing)

Convert XML to Excel Online (https://northconcepts.com/tools/xml-to-excel/)

Input XML le
In this example we will process stock quotes available at Yahoo Finance (http://query.yahooapis.com/v1/public/yql?
q=select%20*%20from%20yahoo. nance.quotes%20where%20symbol%20in%20%28%22YHOO%22%2C%22AAPL%22%2C%22GOO
G%22%2C%22MSFT%22%29%0A%09%09&diagnostics=false&env=http%3A%2F%2Fdatatables.org%2Falltables.env). We saved this
XML data on disk in a le named “yahoo_stock_quote_feed.xml”.

1 <?xml version="1.0" encoding="UTF-8"?>


2 <query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="4" yahoo:created="2015-06-19T10:01:24Z" yahoo
3 <results>
4 <quote symbol="YHOO">
5 <Ask>41.26</Ask>
6 <AverageDailyVolume>14324400</AverageDailyVolume>
7 <Bid>40.66</Bid><AskRealtime/><BidRealtime/>
8 <BookValue>35.91</BookValue>
9 <Change_PercentChange>-0.05 - -0.12%</Change_PercentChange>
10 <Change>-0.05</Change>
11 <Commission/>
12 <Currency>USD</Currency>

Java code
1 package com.northconcepts.datapipeline.examples.cookbook.blog;
2  
3 import java.io.File;
4  
5 import com.northconcepts.datapipeline.core.DataReader;
6 import com.northconcepts.datapipeline.core.DataWriter;
7 import com.northconcepts.datapipeline.core.FieldType;
8 import com.northconcepts.datapipeline.excel.ExcelDocument;

https://northconcepts.com/blog/2015/06/22/convert-xml-to-excel/ 1/6
05/10/2017 How to convert XML to Excel - Data Pipeline

9 import com.northconcepts.datapipeline.excel.ExcelWriter;
10 import com.northconcepts.datapipeline.job.JobTemplate;
11 import com.northconcepts.datapipeline.transform.BasicFieldTransformer;
12 import com.northconcepts.datapipeline.transform.MoveFieldBefore;
13 import com.northconcepts.datapipeline.transform.RemoveFields;
14 import com.northconcepts.datapipeline.transform.SetCalculatedField;
15 import com.northconcepts.datapipeline.transform.TransformingReader;
16 import com.northconcepts.datapipeline.xml.XmlReader;
17  
18 public class ConvertAnXmlFileToAnExcelFile {
19  
20
21
22 public static void main(String[] args)
23 {
24 /*
25 * 1) Define a reader from an XML file
26 */
27 DataReader xmlReader = new XmlReader(new File("example/data/input/yahoo_stock_quote_feed.xml"))
28 .addField("company", "//quote/Name")
29 .addField("ticker", "//quote/@symbol")
30 .addField("EBITDA", "//quote/EBITDA")
31 .addField("last trade date", "//quote/LastTradeDate")
32 .addField("currency", "//quote/Currency")
33 .addField("ask", "//quote/Ask")
34 .addField("bid", "//quote/Bid")
35 .addField("day range", "//quote/DaysRange")
36 .addRecordBreak("//quote");
37  
38 /*
39 * 2) Transform the input data on-the-fly (optional)
40 */
41 DataReader transformingReader = new TransformingReader(xmlReader)
42 .add(
43  
44 new BasicFieldTransformer("EBITDA")
45 .replaceAll("M", "E6")
46 .replaceAll("B", "E9")
47 .stringToDouble()
48 .numberToInt(),
49  
50 new BasicFieldTransformer("last trade date")
51 .stringToDate("M/d/y"),
52
53 new BasicFieldTransformer("bid")
54 .stringToDouble(),
55
56 new BasicFieldTransformer("ask")
57 .stringToDouble(),
58
59 new SetCalculatedField("bid-ask spread", "bid-ask"),
60
61 new RemoveFields("ask"),
62
63 new MoveFieldBefore("bid-ask spread", "day range"),
64
65 new BasicFieldTransformer("day range")
66 .replaceAll(" - ", ", ")
67 .prepend("[")
68 .append("]")
69 );
70  
71 /*
72 * 3) Define a writer to an Excel file
73 */
74 ExcelDocument excelDocument = new ExcelDocument(); //default is .xlsx format (Excel 2007+)
75 DataWriter excelWriter = new ExcelWriter(excelDocument)
76 .setSheetName("stocks")
77 .setAutofitColumns(true)
78 .setStyleFormat(FieldType.INT, "0.00E+00")
79 .setStyleFormat(FieldType.DOUBLE, "0.00")
80 .setStyleFormat(FieldType.DATE, "yyyy-mmm-dd, ddd");
81
82  
83 /*
84 * 4) Transfer the data from the XML file to the Excel file and save the result
85 */

https://northconcepts.com/blog/2015/06/22/convert-xml-to-excel/ 2/6
05/10/2017 How to convert XML to Excel - Data Pipeline

86 JobTemplate.DEFAULT.transfer(transformingReader, excelWriter);
87 excelDocument.save(new File("example/data/output/stock_quote_summary.xlsx"));
88 }
89  
90 }

1. De ne a reader from an XML le


XmlReader (https://northconcepts.com/javadocs/com/northconcepts/datapipeline/xml/XmlReader.html) lets you obtain records from
any XML stream.  The XML stream is broken into separate records by calling addRecordBreak(String locationPath) on your
XmlReader instance.   Fields are then added to each record using the addField(String name, String locationPath) method. The
locationPath expressions used in the above methods are a subset of the XPath 1.0 location paths notation
(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/xml/XmlReader.html). Below we give an example of location
paths for selecting a node and an attribute:

"//quote/Company" – selects all Company nodes that are children of a quote node no matter where they are in the document;
"//quote/@symbol" – selects the symbol attribute in all quote nodes no matter where they are in a document.

2. Transform the input data on-the- y (optional)


When converting data between formats, we often need to perform transformations. For example, we may read numbers as text from
an XML le, but prefer to save them as a number in an Excel le. Or, we may need to adjust date and number formatting, and
calculate aggregated values.

TransformingReader (https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/TransformingReader.html) is a
proxy that applies transformations to records being passed through it. On-the- y record transformations are added to a
TransformingReader using the method add(Transformer... transformers) . Data Pipeline makes it easy to:

transform eld values: BasicFieldTransformer


(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/BasicFieldTransformer.html);
add calculated and constant value elds: SetCalculatedField
(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/SetCalculatedField.html), SetField
(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/SetField.html);
modify the eld list: SelectFields
(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/SelectFields.html), MoveFieldAfter
(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/MoveFieldAfter.html), MoveFieldBefore
(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/MoveFieldBefore.html), RemoveFields
(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/RemoveFields.html), CopyField
(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/CopyField.html), RenameField
(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/RenameField.html);
split array elds and lookup data: SplitArrayField
(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/SplitArrayField.html), LookupTransformer
(https://northconcepts.com/javadocs/com/northconcepts/datapipeline/transform/lookup/LookupTransformer.html).

The following posts contain additional on-the- y data-transformation examples: Lookup Data in any Data Reader
(https://northconcepts.com/docs/examples/lookup-data-in-any-data-reader/), Search Twitter for Tweets
(https://northconcepts.com/docs/examples/search-twitter-for-tweets/), Rename a Field
(https://northconcepts.com/docs/examples/rename-a- eld/) and Set a Field (https://northconcepts.com/docs/examples/set-a- eld/).

3. De ne a writer to a Microsoft Excel le


ExcelWriter (https://northconcepts.com/javadocs/com/northconcepts/datapipeline/excel/ExcelWriter.html) lets you write records to a
Microsoft Excel document. It is used in combination with ExcelDocument (https://northconcepts.com/javadocs/index.html?
com/northconcepts/datapipeline/excel/ExcelDocument.html), a class that encapsulates an Excel workbook in memory. Data Pipeline
supports both the XLS and XSLX le format. By default, the ExcelDocument objects represent an XLSX le. You ca use ExcelWriter’s
setStyleFormat(FieldType fieldType, String pattern) method to set the desired cell format per data type.

4. Transfer the data from the XML le to the Excel le and save the result

https://northconcepts.com/blog/2015/06/22/convert-xml-to-excel/ 3/6
05/10/2017 How to convert XML to Excel - Data Pipeline

JobTemplate (https://northconcepts.com/javadocs/index.html?com/northconcepts/datapipeline/job/JobTemplate.html) is a base type


for classes that transfer records from a DataReader to a DataWriter. Data Pipeline provides a default JobTemplate implementation
accessible at JobTemplate.DEFAULT . A call to the method JobTemplate.DEFAULT.transfer(DataReader reader, DataWriter writer)
will do the data transfer.

Finally, a call to the ExcelDocument’s save(File excelFile) method is needed to write the generated workbook data to an Excel le
on disk.

Output le

All examples can be found in the examples/src folder of the download (https://northconcepts.com/downloads/).

Download Data Pipeline (https://northconcepts.com/pricing)

(/#facebook) (/#twitter) (/#google_plus) (/#linkedin) (/#pinterest) (/#pocket)


Share
(https://www.addtoany.com/share#url=https%3A%2F%2Fnorthconcepts.com%2Fblog%2F2015%2F06%2F22%2Fconvert-
xml-to-excel%2F&title=How%20to%20convert%20XML%20to%20Excel)

About The DataPipeline Team


We make Data Pipeline — a lightweight ETL engine for Java. Use it to lter, transform, and aggregate data on-the- y in your web, mobile, and
desktop apps. Learn more about it at northconcepts.com (https://northconcepts.com/). View all posts by The DataPipeline Team →
(https://northconcepts.com/blog/author/northconceptsteam/)

 How to create multiple sheets in a single Excel le (https://no… How to read data in parallel using AsyncMultiReader  (https://…

2 thoughts on “How to convert XML to Excel”


Pingback: How to convert XML to Excel | Dinesh Ram Kali. (https://dineshramitc.wordpress.com/2015/06/24/how-to-convert-xml-to-
excel/)

Pingback: Data Pipeline 3.1 Now Available - Data Pipeline (https://northconcepts.com/blog/2015/07/02/data-pipeline-3-1-now-available/)

Leave a Reply

https://northconcepts.com/blog/2015/06/22/convert-xml-to-excel/ 4/6
05/10/2017 How to convert XML to Excel - Data Pipeline

Your email address will not be published. Required elds are marked *

Name

Email

Website

Comment

You may use these HTML (HyperText Markup Language) tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url="">
<del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title=""
data-url="">

Post Comment

About Data Pipeline


Data Pipeline is a lightweight ETL engine for Java. Use it to lter, transform, and aggregate data on-the- y in your
web, mobile, and desktop apps. Learn More (/)

Top Posts
 6 tips to improve your exception handling (https://northconcepts.com/blog/2013/01/18/6-tips-to-improve-your-exception-handling/)

 Export CSV and Excel from Java web apps (https://northconcepts.com/blog/2012/12/10/export-csv-and-excel-from-java-web-apps-with-


data-pipeline/)

 Import CSV and Excel to Java Web Apps (https://northconcepts.com/blog/2013/08/28/import-csv-and-excel-to-java-web-apps-with-data-


pipeline/)

 Read Excel les (https://northconcepts.com/docs/examples/read-from-an-excel- le/)

 Copy columns from one CSV le to another (https://northconcepts.com/blog/2014/05/24/transfer-columns-one-csv- le-another-using-


java/)

 Read streaming JSON (https://northconcepts.com/docs/examples/read-a-json-stream/)

 Read xed-width/ xed-length record les (https://northconcepts.com/docs/examples/read-a- xed-width- le- xed-length-record- le/)

 Write XML les using FreeMarker templates (https://northconcepts.com/docs/examples/write-an-xml- le-using-freemarker-templates/)

Recent Posts
 24 ETL Tools for Java Developers (https://northconcepts.com/blog/2017/08/31/java-etl-tools/)

 Scala and Data Pipeline – Phone Bill Calculation Example (https://northconcepts.com/blog/2017/08/24/scala-data-pipeline-example/)

 How to Export Emails from Gmail to Excel with Data Pipeline (https://northconcepts.com/blog/2017/08/10/export-emails-to-excel/)

 Spring Batch vs Data Pipeline – ETL Job Example (https://northconcepts.com/blog/2016/10/04/spring-batch-data-pipeline-etl-job-example/)

https://northconcepts.com/blog/2015/06/22/convert-xml-to-excel/ 5/6
05/10/2017 How to convert XML to Excel - Data Pipeline

 20 Conferences Data Scientists Should Attend in 2017 and 2018 (https://northconcepts.com/blog/2016/07/19/20-conferences-data-


scientists-attend/)

Data Pipeline
Home (/)

Features (/features/)

Pricing (/pricing/)

Downloads (/downloads/)

Services (/services/)

Docs
What is Data Pipeline (/docs/what-is-it/)

How it works (/docs/how-it-works/)

Getting Started (/docs/getting-started)

User Guide (/docs/user-guide/#toc0)

Data Formats (/docs/data-formats)

Operators (/docs/operators)

Expression Language (/docs/expression-language)

Examples (/docs/examples)

Javadocs (/javadocs)

FAQ (/docs/faq)

Company
Blog (/blog/)

About Us (/about/)

Contact Us (/contact/)

Twitter (https://twitter.com/NorthConcepts)

Schedule a Demo (/contact/schedule-demo)

Privacy Policy (/privacy/)

Terms of Use (/terms/)

Data Pipeline License (EULA) (/license/commercial/)

Tools
Schedule Data Pipeline Jobs (/manager/)

Generate Data Pipeline Code (/tools/data-pipeline-builder/)

Export Twitter searches to Excel (/tools/twitter-search/)

Export Twitter searches to Excel (Desktop) (/tools/twitter-search/desktop/)

Convert JSON to CSV Online (/tools/json-to-csv/)

Convert XML to CSV Online (/tools/xml-to-csv/)

Convert XML to Excel Online (/tools/xml-to-excel/)

https://northconcepts.com/blog/2015/06/22/convert-xml-to-excel/ 6/6

You might also like