XML parsing in KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML parsing in KSH
# 1  
Old 05-13-2011
Question XML parsing in KSH

Hi All,

Although I was able to find past XML parsing questions, as the questions were a little different than this, I had a pretty hard time adapting those answers to this scenario.

-> May I ask if anyone knows how to extract the IP addresses of the below "servers.xml" file into an array called IP[]?
(Hopefully using KSH in Solaris)
(note: there can be a varying # of server lines [sometimes zero] within this list, and will most likely be formatted on multiple lines, like below--Also note lines 1-2 and the last line below are guaranteed to be there.)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<serverList xmlns="http://www.w3.org/1999/xhtml">
 <server hostname="server01" ip="192.168.1.101" loc="rm1" region="NY"/>
 <server hostname="server02" ip="192.168.1.117" loc="rm1" region="NY"/>
 <server hostname="server03" ip="192.168.1.154" loc="rm1" region="NY"/>
 <server hostname="server04" ip="192.168.1.159" loc="rm2" region="NY"/>
</serverList>

Thanks in advance...
CG
# 2  
Old 05-13-2011
As you may have found, XML parsing can be very complex.

Each case should be treated uniquely.

As for the data sample you provided, try the code below:
Code:
sed -n '/ip="/s/.*ip="\(.*\)" loc.*/\1/p' input_xml

This User Gave Thanks to Shell_Life For This Post:
# 3  
Old 05-14-2011
To get the IPv4 addresses into an array:
Code:
$ set -A myarray `sed -n '/ip="/s/.*ip="\(.*\)" loc.*/\1/p' infile`
$ echo ${myarray[@]}
192.168.1.101 192.168.1.117 192.168.1.154 192.168.1.159

This User Gave Thanks to fpmurphy For This Post:
# 4  
Old 05-16-2011
xmllint is a very handy tool to work with XML files. Here is a foolproof way to get the ip addresss no matter how your XML file is formatted, even it is packed in one line.

Code:
echo "
setns a=http://www.w3.org/1999/xhtml
xpath /a:serverList/a:server[@ip]" | \
	xmllint --shell sample.xml | \
	awk '/ATTRIBUTE ip$/{getline;getline;split($0,a,"=");print a[2]}'

I blog about xmllint sometime ago because I cannot find any useful examples on the internet. See this Chi Hung Chan
This User Gave Thanks to chihung For This Post:
# 5  
Old 05-17-2011
MySQL

Thank you both Shell_Life & fpmurphy!

For those that need an XML parser to work similar to this scenario reading in the future, it turns out a perfect combination of the 2 suggestions above were needed. Thanks guys!

This is what works:
Code:
set -A myarray `sed -n '/ip="/s/.*ip="\(.*\)" ad.*/\1/p' /servers.xml`
echo ${myarray[@]}
192.168.1.101 192.168.1.117 192.168.1.154 192.168.1.159

Edit:
Chi Hung: Just before hitting submit, I noticed your post as well. Thank you! Hm, very interesting. I'll check this out now...especially since I plan to do a lot more XML parsing pretty soon! Thank you!
# 6  
Old 05-19-2011
Sorry to contradict you but the script you came up with is not an XML parser in any shape, fashion or form. It is strictly a shell script. An XML parser is a completely different piece of software.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

XML parsing

I have an xml file where the format looks like below <SESSIONCOMPONENT REFOBJECTNAME ="pre_session_command" REUSABLE ="NO" TYPE ="Pre-session command"> <TASK DESCRIPTION ="" NAME ="pre_session_command" REUSABLE ="NO" TYPE ="Command" VERSIONNUMBER ="1"> ... (8 Replies)
Discussion started by: r_t_1601
8 Replies

2. Shell Programming and Scripting

XML Parsing :

HI I want to parse below file in to two output :- Input :- ?xml version="1.0" encoding="UTF-8"?> <bulkCmConfigDataFile xmlns:un="utranNrm.xsd" <configData dnPrefix="Undefined"> <xn:SubNetwork id="ONRM_ROOT_MO_R"> <xn:MeContext id="C136"> ... (3 Replies)
Discussion started by: asavaliya
3 Replies

3. Shell Programming and Scripting

XML parsing

i have xml output in below format... <AlertsResponse> <Alert id="11216" name="fgdfg"> <AlertActionLog timestamp="1356521629778" user="admin" detail="Recovery Alert"/> </Alert> <Alert id="11215" name="gdfg <AlertActionLog timestamp="1356430119840" user=""... (12 Replies)
Discussion started by: vivek d r
12 Replies

4. Shell Programming and Scripting

XML: parsing of the Google contacts XML file

I am trying to parse the XML Google contact file using tools like xmllint and I even dived into the XSL Style Sheets using xsltproc but I get nowhere. I can not supply any sample file as it contains private data but you can download your own contacts using this script: #!/bin/sh # imports... (9 Replies)
Discussion started by: ripat
9 Replies

5. UNIX for Advanced & Expert Users

XML Parsing

I had a big XML and from which I have to make a layout as below *TOTAL+CB | *CB+FX | CS |*IR | *TOTAL | -------------------------------------------------------------------------------------------------- |CB FX | | | | DMFXNY EMSGFX... (6 Replies)
Discussion started by: manas_ranjan
6 Replies

6. Shell Programming and Scripting

Parsing XML

Learned People, Hello ! Till today, for the most part, all of the tricky questions/situations that I encountered were already posted by other folks and all I had to do was peruse through these one at a time and I could find some sort of an answer and all I had to do was add some minor tweaks... (5 Replies)
Discussion started by: ManoharMa
5 Replies

7. Shell Programming and Scripting

XML parsing

I have a xml file attached. I need to parse parameterId and its value My output should be like 151515 38 151522 32769 and so on.. Please help me. Its urgent (6 Replies)
Discussion started by: LavanyaP
6 Replies

8. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

9. Shell Programming and Scripting

Parsing and getting data from XML file using ksh script

Hi All, I have a xml file for example as described below <xml> <address> <street><street> <address/> <isbn>426728783932020308393930303</isbn> <book> <name> </name> </book> . . . </xml> My problem is to get the isbn number from the above described file using ksh script. Could... (6 Replies)
Discussion started by: vinna
6 Replies

10. Shell Programming and Scripting

parsing xml

I want to use wget comment to parse an xml parse that exist in an online website. How can I connect it using shell script through Unix and how can I parse it?? (1 Reply)
Discussion started by: walnut
1 Replies
Login or Register to Ask a Question