How to convert xml file to text file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert xml file to text file?
# 1  
Old 10-31-2010
Data How to convert xml file to text file?

Hi Everyone,

I want to convert 1 xml file to text file....

Please help in the xml file i have 4 columns....

Thanks a lot in advance.
Nilesh
# 2  
Old 10-31-2010
provide sample and your expect O/P.
# 3  
Old 10-31-2010
This is the sample format from xml

Code:
<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella</author>
      <title>Guide</title>
      <genre>Comp</genre>
      <price>44.95</price>
     </book>

Above is the xml file format and i want this to convert in to pipe delimited file like below ...please help

Code:
author       |title|Genre|Price
Gambardella|Guide|Comp|44.95


Last edited by radoulov; 10-31-2010 at 06:19 AM.. Reason: Code tags, please!
# 4  
Old 10-31-2010
try:
Code:
awk -F '[<>]' 'BEGIN{h="author|title|genre|price";print h}/<book/,/<\/book>/{ if($2~h) printf "%s"($2~/price/?"\n":"|"),$3} ' infile

this simple version would maybe work too, depending on your input file:
Code:
awk -F '[<>]' 'BEGIN{h="author|title|genre|price";print h}{ if($2~h) printf "%s"($2~/price/?"\n":"|"),$3} ' infile


Last edited by Scrutinizer; 10-31-2010 at 06:30 AM..
# 5  
Old 10-31-2010
Thanks but i want to take xml file as a parameter and then want to convert that file into the pipe delimited file
# 6  
Old 10-31-2010
You can replace infile with the name of you file or a variable containing the name.
# 7  
Old 10-31-2010
Here is a general purpose XSL stylesheet which will do what you want
Code:
 <xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="text"/>

   <xsl:template match="catalog">
      <xsl:call-template name="header" />
      <xsl:apply-templates select="book" />
   </xsl:template>

   <xsl:template name="header">
      <xsl:for-each select="//book[1]/child::*" >
         <xsl:value-of select="name()" />
         <xsl:if test="following-sibling::*">|</xsl:if>
      </xsl:for-each>
      <xsl:text>
</xsl:text>
   </xsl:template>

   <xsl:template match="book">
      <xsl:for-each select="child::*" >
         <xsl:value-of select="." />
         <xsl:if test="following-sibling::*">|</xsl:if>
      </xsl:for-each>
      <xsl:text>
</xsl:text>
   </xsl:template>

</xsl:stylesheet>

Suppose you have the following input document
Code:
<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella</author>
      <title>Guide</title>
      <genre>Comp</genre>
      <price>44.95</price>
   </book>
   <book id="bk102">
      <author>Corazon</author>
      <title>Tagalog</title>
      <genre>Langauge</genre>
      <price>24.65</price>
   </book>
</catalog>

Here is the output after transforming the document using the above stylesheet
Code:
$ xsltproc example.xsl example.xml
author|title|genre|price
Gambardella|Guide|Comp|44.95
Corazon|Tagalog|Langauge|24.65
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. UNIX for Beginners Questions & Answers

Convert Excel File (xls) to tab delimited text file on AIX

Hi i have a problem in my job i try to convert an excel file (xls extention) to text file (tab delimited), but no result with this comand cat xxx.xls > xxx.txt Do you have eny idea? PS: sorry for my english Thanks!! (4 Replies)
Discussion started by: frisso
4 Replies

3. 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

4. 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

5. Shell Programming and Scripting

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... (1 Reply)
Discussion started by: qamar.shahbaz
1 Replies

6. Shell Programming and Scripting

How to convert a txt file to xml file

Hello, I donot have exact lenght of the file. But i want entire txt of the file to send it into one node. for example I have txt file.. asdfghjklmnbvcxzqwertyuiop., nswwneikniniwn so i need the output as <documentbody>asdfghjklmnbvcxzqwertyuiop., ... (9 Replies)
Discussion started by: Hemantgupta84
9 Replies

7. Shell Programming and Scripting

how to convert a html file to an .xml one

Hii every one. I have lots of .html files like: <html> <title> </title> <head> </head> <body> <ul id="SummaryRatings">4 stars </ul> <ul id="summaryList"> <p><b> Aurhor</b> </p> <p>wsfwfrfrtgretryetyrtyty</p> </ul> ...... ....... </body> </html> (6 Replies)
Discussion started by: kheyali Mitra
6 Replies

8. 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

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