The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
just want certail elements frenchface Shell Programming and Scripting 1 05-10-2008 06:38 PM
Map - printing all elements - why? dhanamurthy High Level Programming 0 04-14-2008 10:19 AM
Help in extracting only certain elements in file ahjiefreak Shell Programming and Scripting 1 12-07-2007 12:20 AM
seperate elements of a file nektarios4u Shell Programming and Scripting 1 11-02-2007 09:53 AM
How can i read array elements dynamically in bash? haisubbu UNIX for Dummies Questions & Answers 1 08-28-2006 11:19 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-23-2008
Registered User
 

Join Date: Aug 2005
Location: Bangalore
Posts: 196
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 thanks.

for instance:

Quote:
Family1 ---> Father----->JOHN
Family1 --->Mother----->ANN
Family2 ---> Father----->DAVE
Family2 ---> MOM----->LISA
<TREE>
<Family1>
<Father>
<name = John />
</Father>
<Mother>
<name = ANN />
</Mother>
</Family1>

<Family2>
<Father>
<name=DAVE />
</Father>
<Mother>
<name= Lisa />
</Mother>
</Family2>
</TREE>
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 01-23-2008
Moderator
 

Join Date: Dec 2003
Location: /ksh93
Posts: 863
One way is to use sed repeatedly as the following example shows.

Code:
 sed -n '/\<Family2\>/,/\<\/Family2\>/p' file | sed -n '/\<Father\>/,/\<\/Father\>/p' | sed -n 's/\<name=\(.*\)\/\>/\1/p'
It outputs "DAVE".

A better way is to use something like the XML extension to gawk (XMLgawk)
Reply With Quote
  #3 (permalink)  
Old 01-23-2008
Registered User
 

Join Date: Aug 2005
Location: Bangalore
Posts: 196
the above code is not working for me. and also could you please explain me in more details about XMLgawk? Thanks
Reply With Quote
  #4 (permalink)  
Old 01-23-2008
Registered User
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
Does this works?
Code:
sed -n '/Family2/{n;n;s/<name=\(.*\)\/>/\1/p}' /tmp/Input
Thanks
Nagarajan G
Reply With Quote
  #5 (permalink)  
Old 01-23-2008
Registered User
 

Join Date: Aug 2005
Location: Bangalore
Posts: 196
thats not the solution i am looking for, actually the format could be anything like all the data in one line or multiple lines. or in any well formed format of xml. I need a generic solution to pick the element values.

Anyways, Thanks for the efforts you put in it.
Reply With Quote
  #6 (permalink)  
Old 01-23-2008
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,454
I highly recommend using a XML::Parser for it

Its very easy to use and implement.

Initially for one of our tasks, without thinking about XML::Parser, I did wrote a mini XML::Parser which turned to be much complicated to maintain later.

Switch to parsers that are already available
Reply With Quote
  #7 (permalink)  
Old 01-23-2008
Registered User
 

Join Date: Aug 2005
Location: Bangalore
Posts: 196
Quote:
Originally Posted by matrixmadhan View Post
I highly recommend using a XML::Parser for it

Its very easy to use and implement.

Initially for one of our tasks, without thinking about XML::Parser, I did wrote a mini XML::Parser which turned to be much complicated to maintain later.

Switch to parsers that are already available
I am not able to locate any. Could you please point me to any???
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0