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
# 1  
Old 08-16-2010
How to retrieve value from xml tags

hello,

I have a file that have lines that contains xml tags. for each line, i want to retrieve the value from the following xml tags and output it to another file with the values only, comma seperated. what is the best way to do this? again, the string is all in 1 line one, though it has many tags.

input : get the values
-------------------------------
<TCRMService><ResponseControl><ResultCode>SUCCESS</ResultCode><ServiceTime>510</ServiceTime><DWLControl><requesterLanguage>100</requesterLanguage><requesterLocale>en</requesterLocale><requesterName>SITE_DM5_070710</requesterName><requestID>1018757</requestID></DWLControl></ResponseControl><TxResponse><RequestType>addCCMSSite</RequestType><TxResult><ResultCode>SUCCESS</ResultCode></TxResult><ResponseObject><TCRMExtension> <TCRMSiteBObj><ComponentID>1000634</ComponentID><SiteId>819128085544095729</SiteId><SitePartyId>S0030Y3ZL6</SitePartyId><ParentOrganizationPartyId>O000Z4M0W3</ParentOrganizationPartyId><MainSiteIndicator>N</MainSiteIndicator><HeadQuarterIndicator>Y</HeadQuarterIndicator><PreferLanguageType>400</PreferLanguageType><PreferLanguageValue>ä¸*文(简体)</PreferLanguageValue><MainBPSiteIndicator>N</MainBPSiteIndicator><ISUCodeType>1000013</ISUCodeType><ISUCodeValue>Global Midmarket Business</ISUCodeValue><DUNSNumber>654642826</DUNSNumber><SiteUsageType>1000001</SiteUsageType><SiteUsageValue>Sold To</SiteUsageValue><CustomerClassificationType>1000005</CustomerClassificationType><CustomerClassificationValue>Commercial Customer</CustomerClassificationValue><BPSiteIndicator>N</BPSiteIndicator><TCRMSiteLastUpdateDate>2010-08-03 17:10:40.957</TCRMSiteLastUpdateDate><TCRMSiteLastUpdateTxId>380128085544090107</TCRMSiteLastUpdateTxId><TCRMSiteLastUpdateUser>SITE_DM5_070710</TCRMSiteLastUpdateUser><DWLStatus><Status>0</Status></DWLStatus><TCRMExtension> <TCRMSiteNameBObj><ComponentID>1000683</ComponentID><SiteNameId>854128085544099140</SiteNameId>


output: to another file with this format.
Code:
1018757,SUCCESS,S0030Y3ZL6

please see attach response file for example. i'dl like to do this using either awk/sed or perl.

thank you much in advance.

Moderator's Comments:
Mod Comment Generally use code tags, ty.

Last edited by zaxxon; 08-18-2010 at 11:31 AM..
# 2  
Old 08-16-2010
try this,

Code:
perl -lne 'if ( /\<requestID\>(\d+)\<\/requestID\>(.+?)\<ResultCode\>(\w+)\<\/ResultCode\>(.+?)>\<SitePartyId\>(.+?)\<\/SitePartyId\>/ ) { print $1,",",$3,",",$5;}' response.xml

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 08-16-2010
Not really correct, but just for your reference.
Code:
awk '{gsub(/></,">\n<")}1' response.xml|awk -F ">|<" '/requestID/||/SitePartyId/||/ResultCode/ {printf $3","}'

SUCCESS,1018757,SUCCESS,S0030Y3ZL6,

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 08-18-2010
rdcwayx, thanks this might work for me: can you explain the code?

awk '{gsub(/></,">\n<")}1' response.xml|awk -F ">|<" '/requestID/||/SitePartyId/||/ResultCode/ {printf $3","}'

---------- Post updated at 04:56 PM ---------- Previous update was at 10:20 AM ----------

Quote:
Originally Posted by pravin27
try this,

Code:
perl -lne 'if ( /\<requestID\>(\d+)\<\/requestID\>(.+?)\<ResultCode\>(\w+)\<\/ResultCode\>(.+?)>\<SitePartyId\>(.+?)\<\/SitePartyId\>/ ) { print $1,",",$3,",",$5;}' response.xml

=====================================
I am beginning to understand,can you explain your code? I tried this on my system and it works. I even tried a few more attributes, which works but got really confused as to what all the syntax and commands are doing. Your explanation is greatly appreciated.

ie.

what is (.+?) and (\w+) mean?

ultimately, i'd like to put this in a script instead of running this on the command line. i could potentially expand this to 12 attributes which would be cumbersome on the command line. is there a way to put this in a shell script?

---------- Post updated at 05:06 PM ---------- Previous update was at 04:56 PM ----------

Quote:
Originally Posted by rdcwayx
Not really correct, but just for your reference.
Code:
awk '{gsub(/></,">\n<")}1' response.xml|awk -F ">|<" '/requestID/||/SitePartyId/||/ResultCode/ {printf $3","}'

SUCCESS,1018757,SUCCESS,S0030Y3ZL6,

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

I need to redirect the results to a new file with a new line for every input line that got processed. I tried the code, which is bringing back the correct values - but in the output, it is all in 1 single line. how do I insert a new line into the output for each record that got processed? ie.

output should like like this:

SUCCESS,1018757,SUCCESS,S0030Y3ZL6
SUCCESS,1018758,SUCCESS,S0030Y3ZL6
SUCCESS,1018759,SUCCESS,S0030Y3ZL6
SUCCESS,1018760,SUCCESS,S0030Y3ZL6

Also, I need to get rid of the first 'SUCCESS' so the actually output should look like this:

1018757,SUCCESS,S0030Y3ZL6
1018758,SUCCESS,S0030Y3ZL6
1018759,SUCCESS,S0030Y3ZL6
1018760,SUCCESS,S0030Y3ZL6
# 5  
Old 08-18-2010
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
# 6  
Old 08-18-2010
Code:
awk '{gsub(/></,">\n<")}1' 2site.xml|awk -F ">|<" '
/<TCRMService>/{printf "\n"}
/requestID/||/ResultCode/||/SitePartyId/||/AddressUsageType/||/TransportZoneType/ {printf $3","}
END{printf "\n"}'|cut -d, -f2-

This User Gave Thanks to rdcwayx For This Post:
# 7  
Old 08-18-2010
Quote:
Originally Posted by rdcwayx
Code:
awk '{gsub(/></,">\n<")}1' 2site.xml|awk -F ">|<" '
/<TCRMService>/{printf "\n"}
/requestID/||/ResultCode/||/SitePartyId/||/AddressUsageType/||/TransportZoneType/ {printf $3","}
END{printf "\n"}'|cut -d, -f2-

---------
rdcwayx, thank you very much!. this code works great. how can i put this in a shell script? I want to call this script whenever, the system generates the input file. Also, can you explain the code?
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