Parsing XML elements and store them in array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing XML elements and store them in array
# 1  
Old 04-26-2011
Parsing XML elements and store them in array

Hi Friends

Im so confused with using 'for' loop in ksh. I have a xml like the following:

Code:
<serviceProvider>	
			<serviceProviderID>1</serviceProviderID>
			<serviceProviderName>Balesh</serviceProviderName>
			<serviceFeeAmount>30.00</serviceFeeAmount>				
</serviceProvider>
<serviceProvider>	
			<serviceProviderID>1</serviceProviderID>
			<serviceProviderName>Balesh</serviceProviderName>
			<serviceFeeAmount>120.00</serviceFeeAmount>				
</serviceProvider>
<serviceProvider>	
			<serviceProviderID>1</serviceProviderID>
			<serviceProviderName>Balesh</serviceProviderName>
			<serviceFeeAmount>230.00</serviceFeeAmount>				
</serviceProvider>

I need to extract the value of <serviceFeeAmount> and store them in an array.

Please help me in how to use 'for' loop for this scenario, im confused...

Thanks in advance....
# 2  
Old 04-26-2011
Parsing XML values can be complex specially if the tags and values are spread among multiple lines.

This code will extract the value, but it work only if both tags and value are in the same line:
Code:
sed '/serviceFeeAmount/!d;s/.*>\(.*\)<.*/\1/' Inp_File

# 3  
Old 04-26-2011
Yes, but a preprocessing sed can pile lines into the buffer so as to ensure each <serviceProvider>...</serviceProvider> is now one line with no distracting whitespace ('\t' s/b real tab):
Code:
sed '
  s/[ \t]*\([<>]\)[ \t]*/\1/g
 ' | sed '
  /<serviceProvider>/{
    :loop
    /<\/serviceProvider>/s/[ \t]*\n[ \t]*//g
    t
    $b
    N
    b loop
   }
 '

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can we extract specific elements from XML?

Hi, I have a requirement to extract specific element value dynamically from XML message. Here is the sample message: <File> <List> <main> <dir>doc/store834/archive</dir> <count>5</count> </main> <main> <dir>doc/store834/extract</dir> <count>6</count> </main> <main> ... (3 Replies)
Discussion started by: renukeswar
3 Replies

2. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

3. Shell Programming and Scripting

Grouping array elements - possible?

I have a script which takes backup of some configuration files on my server. It does that by using an array which contains the complete path to the files to backup. It copys the files to a pre defined dir. Each "program" has it's own folder, ex. apache.conf is being copied to /predefined... (7 Replies)
Discussion started by: dnn
7 Replies

4. Shell Programming and Scripting

Removing elements from an array

Hi I have two arrays : @arcb= (450,625,720,645); @arca=(625,645); I need to remove the elements of @arca from elements of @arcb so that the content of @arcb will be (450,720). Can anyone sugget me how to perform this operation? The code I have used is this : my @arcb=... (3 Replies)
Discussion started by: rkrish
3 Replies

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

6. Shell Programming and Scripting

Extract only required elements from XML.

Hi , I have an XML like this. <Request> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <version>v44</version><messageId>7247308192</messageId><timeToLive>72000000000</timeToLive> </Request>. I want to extract on version and messageId. As in my output... (13 Replies)
Discussion started by: chetan.c
13 Replies

7. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

8. Shell Programming and Scripting

Help using SED to comment XML elements

I'm trying to write a script to help automate some VERY tedious manual tasks. I have groups of fairly large XML files (~3mb+) that I need to edit. I need to look through the files and parse the XML looking for a certain flag contained in a field. If I find this flag (an integer value) I need... (4 Replies)
Discussion started by: J-Hon
4 Replies

9. Shell Programming and Scripting

Accessing array elements

Hi, My doubt is how to access array elements.. Situation is as below: #!/bin/ksh set -x typeset -i x=0 typeset -i y=0 typeset -i BID=0 typeset -i count=0 while ] ; do x=`expr $x + 1`; hwmgr show scsi > scsi.tmp while read line; do set... (1 Reply)
Discussion started by: mansa
1 Replies

10. Shell Programming and Scripting

Read elements of a xml file??????

Hi, What is a good way to read elements of an xml file? i did try xmllint it doesnt provide a function to output values of a tree. In the below example when i specify from Family2 I need the name of the father then the output should be DAVE. Appreciate any help provided in this regards. Many... (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies
Login or Register to Ask a Question