Solved

Simple shell script to convert XML data to text output.

Posted on 2006-07-14
4
704 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
Comment Utility
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
Comment Utility
Used the perl one as python wasn't on the system

Thanks does the job perfectly.

S.
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now