Solved

Simple shell script to convert XML data to text output.

Posted on 2006-07-14
4
707 Views
Last Modified: 2013-12-26

Hi, I am looking for a simple shell script that will take any xml and output it in a text format easy on the eye...

eg (any xml could be input, and formatted like below).

<?xml version="1.0" encoding="UTF-8"?>
<MyRecord>
 <aaa>Some Data 1</.aaa>
 <bbb>Some Date 2</bbb>
 <ccc>Some Data 3</bbb>
</MyRecord>
<AnotherRecord>
 <xxx>Some Data 4</xxx>
 <yyy>Some Data 5</yyy>
</AnotherRecord>

Would output to the console like

MyRecord
 aaa = Some Data 1
 bbb = Some Data 2
 ccc = Some Data 3
AnotherRecord
 xxx =  Some Data 4
 yyy = Some Data 5

Thanks,
0
Comment
Question by:SimonBlake
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
slyong earned 250 total points
ID: 17118265
If you have python, try xmlcat from http://gnosis.cx/download/Gnosis_Utils-current.tar.gz otherwise if you only has perl try xmlpp from http://software.decisionsoft.com/tools.html
0
 
LVL 7

Author Comment

by:SimonBlake
ID: 17120716
Used the perl one as python wasn't on the system

Thanks does the job perfectly.

S.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17120773
ah, bummer. I saw this question, and got into work to try come up with something in shellscript (dont have Unix at work)
And by the time I did it, it got accepted!

I'd post anyway,
Just one question though, were your xml tags right or should it be this way

<?xml version="1.0" encoding="UTF-8"?>
<MyRecord>                            
 <aaa>Some Data 1</aaa>              
 <bbb>Some Date 2</bbb>              
 <ccc>Some Data 3</ccc>              
</MyRecord>                          
<AnotherRecord>                      
 <xxx>Some Data 4</xxx>              
 <yyy>Some Data 5</yyy>              
</AnotherRecord>                      


If this way, then this is the script I wrote

This not for points (obviously) but I'd thought I'd post what I wrote anyway. Knowing my luck, it probably wouldnt work anyway!

FILENAME="xml.dat"                                                                
                                                                                 
cat $FILENAME | grep -v "<?xml" | while LINE=`line`                              
do                                                                                
        # Check tags on same line                                                
        tag=`echo $LINE | awk -F'>' '{print $1}' | cut -c2-20`                    
        rv=`echo $LINE | grep "</$tag"`                                          
        if [ "$rv" != "" ]                                                        
        then                                                                      
                # Get text from > to <                                           
                val=`echo $LINE | awk -F'>' '{print $2}' | awk -F'<' '{print $1}'`
                echo "$tag = $val"                                                
        else                                                                      
                echo "$LINE"                                                      
        fi                                                                        
done                                                                              

produces this

<MyRecord>        
aaa = Some Data 1
bbb = Some Date 2
ccc = Some Data 3
</MyRecord>      
<AnotherRecord>  
xxx = Some Data 4
yyy = Some Data 5
</AnotherRecord>  

0
 
LVL 7

Author Comment

by:SimonBlake
ID: 17120998
Sorry... that was a typo - assume the tags were correct.  I'll have a play later as at the moment I have a web log file (get the last 30 mins worth and sum by service) query to look at. Thanks for looking at this one tho. I will try it out this afternoon.

S.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 106
mixString challenge 36 134
Expand macro to ask for filename column 8 37
maxBlock challenge 30 145
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question