How to parse an xml file in a shell script

XML file is in continuation, which means tags are not seperated line by lines.

I want to write a shell script which would be able to get a number within a particular tag.

E.g. OrderMgr.sh is a shell script

>sh OrderMgr.sh input.xml tagname

Output: tagname value is 10

I don't want to write a java program to do this thing...

Kindly help me regarding this....

Thanks
Akshay
akshay_n_sAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
Hi Akshay,

#!/bin/bash

value=`sed "s:.*<$2>\([^<]*\)<.*:\1:" $1`
echo "$2 value is $value"

Cheers!
sunnycoder
0
ahoffmannCommented:
> I want to write a shell script which would be able to get a number within a particular tag.
you'll have a lot of difficulties doing that with shell only, I guess impossible
If you use other programs like sed, grep, awk, perl (as Tintin suggested), you have a little chance to do what you want if you're very experianced in using these tools.
XML is a strucutred format, use an XML parser, anything else is a pain even for such a simple example as you described.

perl with XML:Simple would be the simplest and quickest way to do it.
0
HamdyHassanCommented:
akshay,
I already build a simple script for the same goal to help me investigate issues

$ cat a.xml
<?xml version="1.0" encoding="US-ASCII"?>
<trade>
<start_date>2006-09-01T00:00:00.000</start_date>
<end_date>2006-09-08T00:00:00.000</end_date>
<type>PR</type>
</trade>

$ search_xml.ksh a.xml type
searching a.xml for tagname type
PR

$ cat search_xml.ksh
#!/bin/ksh
echo searching $1 for tagname $2
myvalue=`grep "<$2>.*<.$2>" $1 | cut -f2 -d">"| cut -f1 -d"<"`
echo $myvalue


please let me know with any question about that script
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

ahoffmannCommented:
> .. which means tags are not seperated line by lines.
HamdyHassan, did you read that?
0
HamdyHassanCommented:
ahoffmann, I didn't noticed that line, thanks

here is small changes to the script to cover "tags are not seperated line by lines."

$ cat b.xml                
<?xml version="1.0" encoding="US-ASCII"?>
<trade>
<start_date>2006-09-01T00:00:00.000</start_date><end_date>2006-09-08T00:00:00.000</end_date><type>PR</type>
</trade>

$  search_xml.ksh b.xml type
searching b.xml for tagname type
PR

$ cat search_xml.ksh
#!/bin/ksh
echo searching $1 for tagname $2
myvalue=`grep "<$2>.*<.$2>" $1 | sed -e "s/^.*<$2/<$2/" |  cut -f2 -d">"| cut -f1 -d"<"`
echo $myvalue
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
akshay_n_sAuthor Commented:
Hi HamdyHassan,

Script will not work in the following case!

<Order>
       <Some>sdf</Some>
       <Tags>val</Tags>
       <ProductProfile>
            <Service>
                <Attribute Name="no_of_lines">1<\Attribute>
                <Attribute Name="no_of_lines">1<\Attribute>
           </Service>
       <ProductProfile>
       .
       .
       .
Script is not able to search --> Attribute Name="no_of_lines"
Output should be 1
   
0
ahoffmannCommented:
simple, seed previous grep, sed etc. examples, but what if the file looks like:

<Order>
       <Some>sdf</Some>
       <Tags>val</Tags>
       <ProductProfile>
                <Attribute Name="no_of_lines">42<\Attribute>
            <Service>
                <Attribute Name="no_of_lines">1<\Attribute>
                <Attribute Name="no_of_lines">1<\Attribute>
                <Attribute Name="no_of_lines">3<\Attribute><Attribute Name="no_of_lines">4<\Attribute>
                <![CDATA[
                <Attribute Name="no_of_lines">illegal<\Attribute>
                ]->
           </Service>
       <ProductProfile>

use an XML parser!
0
akshay_n_sAuthor Commented:
Thank you very much for ur help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.