How to retrieve value from xml tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to retrieve value from xml tags
# 8  
Old 08-18-2010
Code:
awk '{gsub(/></,">\n<")}1' 2site.xml|awk -F ">|<" '           # the first awk split each record, you can run it directly to understand it:  awk '{gsub(/></,">\n<")}1' 2site.xml
/<TCRMService>/{printf "\n"}               # each record by each line.
/requestID/||/ResultCode/||/SitePartyId/||/AddressUsageType/||/TransportZoneType/ {printf $3","}                     # take the value that you ask for, if you need more, you can add by yourself.
END{printf "\n"}'|cut -d, -f2-               # remove the first value "SUCCESS"

# 9  
Old 08-19-2010
here was my attempt to put it in a shell script:

===================================

#!/bin/sh

ATTR1=SitePartyId
ATTR2=AddressUsageType
ATTR3=TransportZoneType
CLEANSED_DIR=/mdm/data/load/cleansed
INFILE=$CLEANSED/$1

awk ' { gsub(/></,">\n<") } 1' $INFILE | awk -F ">|<" '/<TCRMService>/ { printf "\n" } /requestID/||/ResultCode/||/'$ATTR1'/||/'$ATTR2'/||/'$ATTR3'/ { printf $3"," } END { printf "\n" } ' |cut -d, -f2- |sed -e 's/.$//'

=================
there was an extra comma at the end of each line - so i found this 'sed' command to remove it. also, i need to remove the first blank line that gets inserted into the file before the record writes. best way to remove first blank line?
# 10  
Old 08-19-2010
Quote:
Originally Posted by davidsouk
here is an example of a file that contains 2 records:

I need to pull out the following attributes:

1) requestID
2) ResultCode
3) SitePartyId
4) AddressUsageType ( this has multiple )
5) TransportZoneType ( this has multiple )

with this FINAL output:

1532890,SUCCESS,S0000009Y5,1001400,1000278,1001100,1000278
1532909,SUCCESS,S000010U81,1001100,1000278,1001400,1000278
Here's a Perl one-liner that does the job -

Code:
$
$
$ wc 2site.xml
    2    95 19038 2site.xml
$
$
$ perl -lne 'while (/<(requestID|ResultCode|SitePartyId|AddressUsageType|TransportZoneType)>(.*?)<\//g){$x .= "$2,"}
             $x=~s/^.*?,(.*),/$1/; print $x; $x=""' 2site.xml
1532890,SUCCESS,S0000009Y5,1001400,1000278,1001100,1000278
1532909,SUCCESS,S000010U81,1001100,1000278,1001400,1000278
$
$
$

tyler_durden
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 to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. Shell Programming and Scripting

How to retrieve values from XML file and update them in the same position! PLEASE HELP?

Good Day All Im quiet new to ksh scripting and need a bit of your help. I am attempting to write a script that reads in an XML and extracts certain field values from an XML file. The values are all alphanumeric and consist of two components: e.g "Test 1". I need to to create a script that... (2 Replies)
Discussion started by: JulioAmerica
2 Replies

3. Red Hat

Error: Cannot retrieve repository metadata (repomd.xml) for repository: InstallMedia.

Most of my commands are returning this error on RHEL 6 64 bit: Also I tried installing many sofwtares, but it fails to correctly work. For example I treid installing dos2unix: # rpm -ivh dos2unix-5.3.3-5.ram0.98.src.rpm 1:dos2unix warning: user mockbuild does not... (0 Replies)
Discussion started by: India_2014
0 Replies

4. Shell Programming and Scripting

How to add Xml tags to an existing xml using shell or awk?

Hi , I have a below xml: <ns:Body> <ns:result> <Date Month="June" Day="Monday:/> </ns:result> </ns:Body> i have a lookup abc.txtt text file with below details Month June July August Day Monday Tuesday Wednesday I need a output xml with below tags <ns:Body> <ns:result>... (2 Replies)
Discussion started by: Nevergivup
2 Replies

5. Shell Programming and Scripting

Compare two xml files while ignoring some xml tags

I've got two different files and want to compare them. File 1 : <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record id="38,557"><columns><column><name>orge... (2 Replies)
Discussion started by: Shaishav Shah
2 Replies

6. Shell Programming and Scripting

Shell Command to compare two xml lines while ignoring xml tags

I've got two different files and want to compare them. File 1 : HTML Code: <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record... (1 Reply)
Discussion started by: Shaishav Shah
1 Replies

7. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

8. Shell Programming and Scripting

How to retrieve the value from XML tag whose end tag is in next line

Hi All, Find the following code: <Universal>D38x82j1JJ </Universal> I want to retrieve the value of <Universal> tag as below: Please help me. (3 Replies)
Discussion started by: mjavalkar
3 Replies

9. Shell Programming and Scripting

how to retrieve specific parameters using a xml tag

Hi, I have the following code in my xml file: <aaaRule loginIdPattern=".*" orgIdPattern=".*" deny="false" /> <aaaRuleGroup name="dpaas"> <aaaRule loginIdPattern=".*" orgIdPattern=".*" deny="false" /> I want to retrieve orgIdPattern and loginIdPattern parameter value based on... (2 Replies)
Discussion started by: mjavalkar
2 Replies

10. Shell Programming and Scripting

awk to retrieve the particular value from a same list of xml tags

Hi All, I have the following code in one of my xml file: <com:parameter> <com:name>secretKey</com:name> <com:value>31XA874821172E89B00B1C</com:value> </com:parameter> <com:parameter> <com:name>tryDisinfect</com:name> <com:value>false</com:value> </com:parameter> <com:parameter>... (4 Replies)
Discussion started by: mjavalkar
4 Replies
Login or Register to Ask a Question