Convert XML file to CSV file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert XML file to CSV file
# 1  
Old 05-08-2012
Convert XML file to CSV file

Hi Guys,
I am new to Shell scripting and need to convert an XML files to a CSV file.
My actual problem is that XML file loading is taking hours and I have decided to convert the XML structure to row based data in a CSV file.

My XML file: Message846 can repeat within main loop and StoredItems can repeat within Message846
Code:
<?xml version="1.0"?>
<Message846_batch>
  <Message846>
    <GeneralInformation>
      <MessageType>Inventory Report</MessageType>
      <MessageSeqNumber>21476</MessageSeqNumber>
      <MessageDate>04/03/2012</MessageDate>
    </GeneralInformation>
    <EntityLocation>
      <Name>FP52</Name>
      <LocationNumber>FP52</LocationNumber>
      <Address1>ROUTE 19</Address1>
      <Address2>Lala mus</Address2>
      <City>XYZ</City>
      <StateCode>PA</StateCode>
      <ZipCode>16066</ZipCode>
      <CountryCode>US</CountryCode>
    </EntityLocation>
    <StoredItems>
      <aMaterialCode>MD272LL/A</MaterialCode>
      <Sku>65495</Sku>
      <UPC>885909528882</UPC>
      <UnusableQty>0</UnusableQty>
      <QtyAvailableForSale>5</QtyAvailableForSale>
      <ShippedQty>0</ShippedQty>
      <DamagedGoods>0</DamagedGoods>
      <ReturnedQty>0</ReturnedQty>
      <BackorderQty>0</BackorderQty>
    </StoredItems>
    <StoredItems>
      <MaterialCode>MD197LL/A</MaterialCode>
      <Sku>65489</Sku>
      <UPC>885909510269</UPC>
      <UnusableQty>0</UnusableQty>
      <QtyAvailableForSale>3</QtyAvailableForSale>
      <ShippedQty>0</ShippedQty>
      <DamagedGoods>0</DamagedGoods>
      <ReturnedQty>0</ReturnedQty>
      <BackorderQty>0</BackorderQty>
    </StoredItems>
  </Message846>
</Message846_batch>

Expected output:
Code:
MessageType|MessageSeqNumber|MessageDate|Name|LocationNumber|Address1|Address2|City|StateCode|ZipCode|CountryCode|MaterialCode|ATTSku|UPC|UnusableQty|QtyAvailableForSale|ShippedQty|DamagedGoods|ReturnedQty|BackorderQty

It would be de-normalized data meaning GeneralInformation and EntityLocation will repeat for StoredItems data.

A detail response would be highly appreciated, I have heard command names like awk and grep but have no idea how to use them.

Thanks in Advance

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 05-08-2012 at 06:21 PM..
# 2  
Old 05-08-2012
Hi qamar.shahbaz,

Did you solve it?

First, you only posted the header. In the XML file, will there be many <Message846> elements and the CSV will have one (or several, see second point) line for each one?

Second, in your example, GeneralInformation and EntityLocation data should be repeated twice because there are two StoredItems elements, do you mean this with de-normalized?

Third, CSV files are separated with commas, yours seems to be separated with pipes, is like this?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert CSV file to nested XML file using UNIX/PERL?

we have a CSV which i need to convert to XML using Perl or Unix shell scripting. I was able to build this XML in oracle database. However, SQL/XML query is running for long time. Hence, I'm considering to write a Perl or shell script to generate this XML file. Basically need to build this XML... (3 Replies)
Discussion started by: laknar
3 Replies

2. Shell Programming and Scripting

How to convert xml to csv ?

I am in need of converting billions of XML into csv file to load data to DB, i have found the below code in perl but not sure why it's not working properly. CODE: #!/usr/bin/perl # Script to illustrate how to parse a simple XML file # and pick out all the values for a specific element, in... (1 Reply)
Discussion started by: rspwilliam
1 Replies

3. Shell Programming and Scripting

How to convert excel file to csv file or text file?

Hi all, I need to find a way to convert excel file into csv or a text file in linux command. The reason is I have hundreds of files to convert. Another complication is the I need to delete the first 5 lines of the excel file before conversion. so for instance input.xls description of... (6 Replies)
Discussion started by: johnkim0806
6 Replies

4. Shell Programming and Scripting

convert huge .xml file in .csv with specific column.

I have huge xml file in server and i want to convert it to .csv with specific column ... i have search in blog but i didn't get any usefully command. Thanks in advance (1 Reply)
Discussion started by: pareshkp
1 Replies

5. Shell Programming and Scripting

Convert CSV file (with double quoted strings) to pipe delimited file

Hi, could some help me convert CSV file (with double quoted strings) to pipe delimited file: here you go with the same data: 1,Friends,"$3.99 per 1,000 listings",8158here " 1,000 listings " should be a single field. Thanks, Ram (8 Replies)
Discussion started by: Ram.Math
8 Replies

6. Shell Programming and Scripting

Convert XML file into TEXT file using PERL seript

Dear Yogesh..."Convert XML file into TEXT file using PERL seript"... can u help me regarding this issue...plz Thanks Rudro (0 Replies)
Discussion started by: Rudro
0 Replies

7. Shell Programming and Scripting

Help to convert XML to CSV

Apologies if this has already been covered in this site somewhere, I did try looking but without any success. I am new to the whole XML thing, very late starter, and have a requirement to convert an XML fiule to a CSV fomat. I am crrently working on a Solaris OS. Does anyone have any suggestions,... (2 Replies)
Discussion started by: rossingi_33
2 Replies

8. Shell Programming and Scripting

Sample Unix script file to convert .xml to .csv

Dear all, Can you send me a script file the changes .xml to .csv file. Thanks, Srinivasa (4 Replies)
Discussion started by: srinivasaphani
4 Replies

9. Shell Programming and Scripting

Plz Help To convert xml file to text file using bourn shell scripts

If someone out there could help me out with this problem. I would really appreciate it. I am trying to convert xml into text file(fixed length) using Unix Borne shell scripts. My xml file: <root> <header_rec recordtype="00"> <record_id>00</record_id> <country_code>AK></country_code>... (0 Replies)
Discussion started by: ram2s2001
0 Replies

10. Shell Programming and Scripting

convert XML file into Text file(fixed length)

If someone out there could help me out with this problem. I would really appreciate it. I am trying to convert xml into text file(fixed length) using Unix Borne shell scripts. My xml file: <root> <header_rec recordtype="00"> <record_id>00</record_id> ... (0 Replies)
Discussion started by: ram2s2001
0 Replies
Login or Register to Ask a Question