Sponsored Content
Full Discussion: XML parsing in KSH
Top Forums Shell Programming and Scripting XML parsing in KSH Post 302523059 by chatguy on Tuesday 17th of May 2011 05:59:03 PM
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!
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
XML::Smart::FAQ(3pm)					User Contributed Perl Documentation				      XML::Smart::FAQ(3pm)

NAME
XML::Smart::FAQ - Frequently Asked Questions about XML::Smart. SYNOPSIS
This is the Frequently Asked Questions list for XML::Smart. QUESTIONS
For new questions send an e-mail to the author, but please, read first all the F.A.Q. Do I need to install XML::Parser to can use XML::Smart? No! XML::Smart already comes with 2 independent parsers, XML::Smart::Parser and XML::Smart::HTMLParser. If XML::Parser is found XML::Smart will use it by default, and the 2nd options will be XML::Smart::Parser. Note that for complex parsing XML::Parser is recommended, but XML::Smart::Parser will work fine too. What is the best version of XML::Smart to install? Is always the last! Always take a look for new versions before aks for help on XML::Smart. Note that internally XML::Smart is complex, since the main idea of it is to remove the complexity from the hand of the programmer. Actually the idea is to enable the Perl programmer to use and create XML data without really know the XML format. Where can I learn about XML? http://www.xml.com How to apply a DTD to a XML::Smart object tree? Take a look in the method apply_dtd(). Example of use: $xml->apply_dtd(q` <!DOCTYPE cds [ <!ELEMENT cds (album+)> <!ATTLIST cds creator CDATA date CDATA #REQUIRED > <!ELEMENT album (artist , tracks+)> <!ELEMENT artist (#PCDATA)> <!ELEMENT tracks (#PCDATA)> ]> `); This will format automatically elements, attributes, etc... How XML::Smart works? To create XML::Smart, first I have created the module Object::MultiType. With it you can have an object that works at the same time as a HASH, ARRAY, SCALAR, CODE & GLOB. So you can do things like this with the same object: $obj = Object::MultiType->new() ; $obj->{key} ; $obj->[0] ; $obj->method ; @l = @{$obj} ; %h = %{$obj} ; &$obj(args) ; print $obj "send data " ; Seems to be crazy, and can be more if you use tie() inside it, and this is what XML::Smart does. For XML::Smart, the access in the Hash and Array way paste through tie(). In other words, you have a tied HASH and tied ARRAY inside it. This tied Hash and Array work together, soo you can access a Hash key as the index 0 of an Array, or access an index 0 as the Hash key: %hash = ( key => ['a','b','c'] ) ; $hash->{key} ## return $hash{key}[0] $hash->{key}[0] ## return $hash{key}[0] $hash->{key}[1] ## return $hash{key}[1] ## Inverse: %hash = ( key => 'a' ) ; $hash->{key} ## return $hash{key} $hash->{key}[0] ## return $hash{key} $hash->{key}[1] ## return undef The best thing of this new resource is to avoid wrong access to the data and warnings when you try to access a Hash having an Array (and the inverse). Thing that generally make the script die(). Once having an easy access to the data, you can use the same resource to create data! For example: ## Previous data: <hosts> <server address="192.168.2.100" os="linux" type="conectiva" version="9.0"/> </hosts> ## Now you have {address} as a normal key with a string inside: $XML->{hosts}{server}{address} ## And to add a new address, the key {address} need to be an ARRAY ref! ## So, XML::Smart make the convertion: ;-P $XML->{hosts}{server}{address}[1] = '192.168.2.101' ; ## Adding to a list that you don't know the size: push(@{$XML->{hosts}{server}{address}} , '192.168.2.102') ; ## The data now: <hosts> <server os="linux" type="conectiva" version="9.0"/> <address>192.168.2.100</address> <address>192.168.2.101</address> <address>192.168.2.102</address> </server> </hosts> Than after changing your XML tree using the Hash and Array resources you just get the data remade (through the Hash tree inside the object): my $xmldata = $XML->data ; But note that XML::Smart always return an object! Even when you get a final key. So this actually returns another object, pointhing (inside it) to the key: $addr = $XML->{hosts}{server}{address}[0] ; ## Since $addr is an object you can TRY to access more data: $addr->{foo}{bar} ; ## This doens't make warnings! just return UNDEF. ## But you can use it like a normal SCALAR too: print "$addr " ; $addr .= ':80' ; ## After this $addr isn't an object any more, just a SCALAR! When I generate the XML data new lines ( ) are added to the content! You should use the options for the method data() and save() to not add identation to the generated data: $XML->data( noident => 1 ) ; ## or better: $XML->data( nospace => 1 ) ; Your question is not here? Just send me an e-mail. ;-P AUTHOR
Graciliano M. P. <gm@virtuasites.com.br> I will appreciate any type of feedback (include your opinions and/or suggestions). ;-P Enjoy and thanks for who are enjoying this tool and have sent e-mails! ;-P ePod This document was written in ePod (easy-POD), than converted to POD, and from here you know the way. perl v5.10.1 2004-12-08 XML::Smart::FAQ(3pm)
All times are GMT -4. The time now is 10:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy