Solved

Simple shell script to convert XML data to text output.

Posted on 2006-07-14
4
709 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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
Error on moodle after upgrade 3 156
Perl Awk Need Help 3 150
sum67 challenge 35 130
Fibonacci challenge 11 141
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

710 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