Solved

Simple shell script to convert XML data to text output.

Posted on 2006-07-14
4
705 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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