parse xml using shell script and store in an array

Hi ,

How can I parse the below xml and store the values in arrays using shell script
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2010 rel. 2 (http://www.altova.com)-->
<Websites xsi:noNamespaceSchemaLocation="sample.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<webapp>
	<ip>192.168.15.47</ip>
	<domainName>www.example1.com</domainName>
	<server>Linux</server>
	<instanceCount>2</instanceCount>
	</webapp>
	<webapp>
	<ip>192.168.15.87</ip>
	<domainName>www.example2.com</domainName>
	<server>Solaris</server>
	<instanceCount>3</instanceCount>
	</webapp>
	<webapp>
	<ip>192.168.15.87</ip>
	<domainName>www.example3.com</domainName>
	<server>Unix</server>
	<instanceCount>4</instanceCount>
	</webapp>
</Websites>

Open in new window

LVL 1
CharanRAsked:
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.

tty2Commented:
1. Retrieve fields from xml file to file "test1".
2. Delete trailing blank spaces in "test1".
3. Read contents of "test1" into array "$array".
5... Test contents of $array.
sed -n -e 's_<[^>]*>\([^<>]*\)<[^>]*>_\1_p' test.xml > test1
sed -e 's/^[ \t]*//;s/[ \t]*$//' test1
i=0; while read line; do array[i++]=$line; done < test1

echo ${array[0]}
192.168.15.47

echo ${array[1]}
www.example1.com

for ((i=0;i<12;i++)); do echo ${array[i]}; done
192.168.15.47
www.example1.com
Linux
2
192.168.15.87
www.example2.com
Solaris
3
192.168.15.87
www.example3.com
Unix
4

Open in new window

0
tty2Commented:
Sorry, there is mistake in line 2: there is parameter "-i" needed:
sed -n -e 's_<[^>]*>\([^<>]*\)<[^>]*>_\1_p' test.xml > test1
sed -i -e 's/^[ \t]*//;s/[ \t]*$//' test1
i=0; while read line; do array[i++]=$line; done < test1

Open in new window

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
CharanRAuthor Commented:
Thanks tty2
How can I store these values in 3 different arrays like 1st array has values of only domainName, 2nd array has values of server and 3rd array instanceCount.

Thanks
0
tty2Commented:
Arrays are named ar_domainName, ar_instanceCount, etc.
You may check the names with command "set".
sed -n -e 's_\([^<>]*\)<[^>]*>_#\1#_;s/[ \t]//;s/#.*#//;/^$/d;s/>//;s_</_ _p' test.xml > test1
mkdir /tmp/test
while read a b; do echo $a >> "/tmp/test/$b"; done < test1
cd /tmp/test
for i in *; do sed -i -e "s/^/]=/" $i; sed -i = $i; sed -i 'N;s/\n//' $i; sed -i "s/^/ar_$i[/" $i; source $i; done
cd -
rm -rf /tmp/test/

set
...
ar_domainName=([1]="www.example1.com" [2]="www.example2.com" [3]="www.example3.com")
ar_instanceCount=([1]="2" [2]="3" [3]="4")
ar_ip=([1]="192.168.15.47" [2]="192.168.15.87" [3]="192.168.15.87")
ar_server=([1]="Linux" [2]="Solaris" [3]="Unix")
...

Open in new window

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
Shell Scripting

From novice to tech pro — start learning today.