[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1402
  • Last Modified:

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

0
CharanR
Asked:
CharanR
  • 3
3 Solutions
 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now