Link to home
Start Free TrialLog in
Avatar of SimonBlake
SimonBlake

asked on

Simple shell script to convert XML data to text output.


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,
ASKER CERTIFIED SOLUTION
Avatar of slyong
slyong

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SimonBlake
SimonBlake

ASKER

Used the perl one as python wasn't on the system

Thanks does the job perfectly.

S.
Avatar of rockiroads
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>  

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.