Extract only required elements from XML.


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

Quote:
Originally Posted by chetan.c
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.
Then, provide us the exact input and expected output which will full fill your requirement.
# 9  
Old 02-29-2012
Do you mean like this:
Code:
awk '/^version|^messageId/{getline p;p=RS $0 RS p;sub(ORS,x,p);print p}' RS=\< infile


Last edited by Scrutinizer; 02-29-2012 at 10:12 AM..
# 10  
Old 02-29-2012
Quote:
Originally Posted by balajesuri
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?
Sorry balaji. I'm new to UNIX. Perl i thought was something different.
So did not try that.

But i tried it now and it works exactly how i require it.

Apologies for that.
can you please explain me what it is doing?As when i search for ".*?" i dont get answers for what is means.

Sorry for skipping it againSmilie

Thanks.

---------- Post updated at 08:31 AM ---------- Previous update was at 08:20 AM ----------

Quote:
Originally Posted by Scrutinizer
Do you mean like this:
Code:
awk '/version|messageId/{getline p;print RS $0 RS p}' RS=\< infile


Hi,

It works fine but problem is in the beginning of xml there is xml version="1.0" encoding="utf-8" which is also being picked up.

Thanks.

---------- Post updated at 08:34 AM ---------- Previous update was at 08:31 AM ----------

Quote:
Originally Posted by balajesuri
Code:
perl -lne 'print $& while (/(<version>.*?<\/version>)|(<messageId>.*?<\/messageId>)/g)' inputfile


Hi Balaji,

This is working perfectly.But in cases like below where ns2: is present and is never constant, how can i pick it up?
Code:
 
<ns2:version>v52<ns2:/version>

Thanks,
Chetan.C
# 11  
Old 02-29-2012
@chetan.c corrected it in my post.
# 12  
Old 02-29-2012
Assuming a sample data as below..try
Code:
[mkt@michael]$ cat inputfile
<Request>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<version>v44</version><messageId>7247308192</messageId><timeToLive>72000000000</timeToLive>
</Request>.
<Request>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<version>2323244</version>
<messageId>724</messageId><timeToLive>72000000000</timeToLive>
</Request>.

[mkt@michael]$ awk '{if(/<version>/&&/messageId/) {print $1 FS $2 FS RS $3 FS $4 FS} else if (/<version>/) {l=$0; getline ;print l RS $1 FS $2 FS } else next}' FS='>' inputfile
<version>v44</version>
<messageId>7247308192</messageId>
<version>2323244</version>
<messageId>724</messageId>

# 13  
Old 03-01-2012
try to explore command

xmllint

* in Linux it is working perfect, pls check your OS before looking into it.
# 14  
Old 03-01-2012
If you install perl-XML-XPath, you will have a perl script called /usr/bin/xpath. Then you can try this
Code:
# xpath inputfile '//version|//messageId' 2>/dev/null
<version>v44</version><messageId>7247308192</messageId>

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