How To Convert XML To Excel - Data Pipeline
How To Convert XML To Excel - Data Pipeline
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).
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=https%3A%2F%2Fptop.only.wip.la%3A443%2Fhttp%2Fdatatables.org%2Falltables.env). We saved this
XML data on disk in a le named “yahoo_stock_quote_feed.xml”.
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 }
"//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.
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:
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/).
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
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/).
How to create multiple sheets in a single Excel le (https://no… How to read data in parallel using AsyncMultiReader (https://…
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
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
Top Posts
6 tips to improve your exception handling (https://northconcepts.com/blog/2013/01/18/6-tips-to-improve-your-exception-handling/)
Read xed-width/ xed-length record les (https://northconcepts.com/docs/examples/read-a- xed-width- le- xed-length-record- le/)
Recent Posts
24 ETL Tools for Java Developers (https://northconcepts.com/blog/2017/08/31/java-etl-tools/)
How to Export Emails from Gmail to Excel with Data Pipeline (https://northconcepts.com/blog/2017/08/10/export-emails-to-excel/)
https://northconcepts.com/blog/2015/06/22/convert-xml-to-excel/ 5/6
05/10/2017 How to convert XML to Excel - Data Pipeline
Data Pipeline
Home (/)
Features (/features/)
Pricing (/pricing/)
Downloads (/downloads/)
Services (/services/)
Docs
What is Data Pipeline (/docs/what-is-it/)
Operators (/docs/operators)
Examples (/docs/examples)
Javadocs (/javadocs)
FAQ (/docs/faq)
Company
Blog (/blog/)
About Us (/about/)
Contact Us (/contact/)
Twitter (https://twitter.com/NorthConcepts)
Tools
Schedule Data Pipeline Jobs (/manager/)
https://northconcepts.com/blog/2015/06/22/convert-xml-to-excel/ 6/6