Extract only required elements from XML.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract only required elements from XML.
# 1  
Old 02-29-2012
Extract only required elements from XML.

Hi ,

I have an XML like this.
Code:
<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 should be
Code:
<version>v44</version>
<messageId>7247308192</messageId>

Not necessarily will the version and messgaeID be in the same line.

Thanks,
Chetan

Last edited by Franklin52; 02-29-2012 at 07:29 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-29-2012
awk

Hi,

Try this one,

Code:
nawk '$0 ~ /\<Request\>/{c=1;next;}$0 ~ /\<\/Request\>/{c=0;next;}$0 !~ /^\<SOAP/ {if(c==1){gsub(/\>\</,">\n<");}print $0;}' Input_File

Cheers,
RangaSmilie
# 3  
Old 02-29-2012
Hi Ranga,

Its not giving the required output.
# 4  
Old 02-29-2012
awk

Quote:
Originally Posted by chetan.c
Hi Ranga,

Its not giving the required output.
what's your output ?
if your system doesn't support nawk then use awk instead of that.

Yeah i have missed, use the below code.

Code:
nawk '$0 ~ /\<Request\>/{c=1;next;}$0 ~ /\<\/Request\>/{c=0;next;}$0 !~ /^\<SOAP/ {if(c==1){gsub(/\>\</,">\n<");gsub(/\<timeToLive\>.*\<\/timeToLive\>\n/,"");}print $0;}' file1

Cheers,
RangaSmilie

Last edited by rangarasan; 02-29-2012 at 07:26 AM..
# 5  
Old 02-29-2012
Code:
perl -lne 'print $& while (/(<version>.*?<\/version>)|(<messageId>.*?<\/messageId>)/g)' inputfile

# 6  
Old 02-29-2012
Hi Ranga,
Yes i used AWK instead of nawk.
But still the output is entire file.

I want to search the tag (<version> ) and pick up data till its closing tag(</version>).
The xml does not always start with request tag.Its random and not structured in the sense that there are also blank spaces in it.

Thanks.

---------- Post updated at 07:53 AM ---------- Previous update was at 07:26 AM ----------

Hi All,

Any thoughts on the above problem?

Thanks.
# 7  
Old 02-29-2012
I reckon your system has perl installed in it. I hope you did try the solution in post #5 above before you bumped posts, didn't you?
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

Extract strings from XML files and create a new XML

Hello everybody, I have a double mission with some XML files, which is pretty challenging for my actual beginner UNIX knowledge. I need to extract some strings from multiple XML files and create a new XML file with the searched strings.. The original XML files contain the source code for... (12 Replies)
Discussion started by: milano.churchil
12 Replies

3. Shell Programming and Scripting

Extract a particular xml only from an xml jar file

Hi..need help on how to extract a particular xml file only from an xml jar file... thanks! (2 Replies)
Discussion started by: qwerty000
2 Replies

4. Shell Programming and Scripting

Help required in Splitting a xml file into multiple and appending it in another .xml file

HI All, I have to split a xml file into multiple xml files and append it in another .xml file. for example below is a sample xml and using shell script i have to split it into three xml files and append all the three xmls in a .xml file. Can some one help plz. eg: <?xml version="1.0"?>... (4 Replies)
Discussion started by: ganesan kulasek
4 Replies

5. Shell Programming and Scripting

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: <serviceProvider> <serviceProviderID>1</serviceProviderID> <serviceProviderName>Balesh</serviceProviderName> <serviceFeeAmount>30.00</serviceFeeAmount> </serviceProvider>... (2 Replies)
Discussion started by: balesh
2 Replies

6. Shell Programming and Scripting

How to extract the required lines?

Hi Techies, I got struck in middle of a problem. I have the following data whose format is: a 1 a 2 a 3 a 4 b 1 b 2 b 3 b 4 c 1 c 2 c 3 c 4 I wanted the output from this to be: a 3 a 4 (11 Replies)
Discussion started by: 14341
11 Replies

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

8. Shell Programming and Scripting

How to extract elements using Awk

Hi, I have this typical extraction problem in AWK. I have 3 input files.. i) First one is somehow like an oracle of:- foo 12,23,24 bla 11,34 car 35 ii)Second file is basically detailing the score for each of the second field of first file. Besides, for the first column, it is the... (3 Replies)
Discussion started by: ahjiefreak
3 Replies

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

10. Shell Programming and Scripting

How to extract elements in a field using a number

Hi, I face difficulty where the number which I grep before I would like to use it as number to grep again in another file. For example in file 1, I extract the second field and assign to variable "char" in a while loop. And then, I grep again this char to get i and j. char=`echo "${LINE}"|... (17 Replies)
Discussion started by: ahjiefreak
17 Replies
Login or Register to Ask a Question