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

<?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:

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

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?