How can we extract specific elements from XML?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How can we extract specific elements from XML?
# 1  
Old 01-04-2017
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:

Code:
<File> 
<List> 
<main> 
<dir>doc/store834/archive</dir> 
<count>5</count> 
</main> 
<main> 
<dir>doc/store834/extract</dir> 
<count>6</count> 
</main> 
<main> 
<dir>doc/store834/normal</dir> 
<count>7</count> 
</main> 
</List> 
</File>

My code is :
Code:
sed -n '/^<dir>/p' filename.xml | head -1

above code is working for only first element, if I want to fetch the second element using "head -2" it returns two elements from top to bottom in a single call, can you please help me with the solution.

Thanks in advance.
Regards
Renukeswar


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-04-2017 at 07:34 AM.. Reason: Added CODE tags.
# 2  
Old 01-04-2017
For exactly your request and input data, try
Code:
grep "^<dir" file | tail -n+2 | head -1
<dir>doc/store834/extract</dir>

This works as the desired data is in one single line only, and it is not an "XML problem". For requests dealing with e.g. several layered structures extending across more than one line, there are better suited tools out there to handle xml structures.

Last edited by RudiC; 01-04-2017 at 07:46 AM..
# 3  
Old 01-04-2017
Try:
Code:
awk -v cnt=2 -v elmt=dir '$1==elmt && ++c==cnt{print $2}' RS=\< FS=\> file

# 4  
Old 01-04-2017
For more-difficult XML, where tags aren't necessarily one clean set per line, you can try my generic XML script, which with GNU awk you would use like

Code:
$ awk -f yanx.awk -e 'TAG == "DIR" { print $2 }' ORS="\n" input.xml

doc/store834/archive
doc/store834/extract
doc/store834/normal

$

On mawk, you would put TAG == "DIR" { print $2 } in a file and do
Code:
awk -f yanx.awk -f tag.awk ORS="\n" input.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract the specific tags in a XML file

Hello Shell Gurus, I have a requirement to get the specific tags from a XML file. Here is my code snippet <jdbc-system-resource> <name>SDPData Source</name> <target>AdminServer,osb_server1,soa_server1</target> ... (30 Replies)
Discussion started by: Siv51427882
30 Replies

2. Shell Programming and Scripting

Parse xml in shell script and extract records with specific condition

Hi I have xml file with multiple records and would like to extract records from xml with specific condition if specific tag is present extract entire row otherwise skip . <logentry revision="21510"> <author>mantest</author> <date>2015-02-27</date> <QC_ID>334566</QC_ID>... (12 Replies)
Discussion started by: madankumar.t@hp
12 Replies

3. Shell Programming and Scripting

Extract only required elements from XML.

Hi , I have an XML like this. <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... (13 Replies)
Discussion started by: chetan.c
13 Replies

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

5. UNIX for Dummies Questions & Answers

Extract a specific number from an XML file based on the start and end tags

Hello People, I have the following contents in an XML file ........... ........... .......... ........... <Details = "Sample Details"> <Name>Bob</Name> <Age>34</Age> <Address>CA</Address> <ContactNumber>1234</ContactNumber> </Details> ........... ............. .............. (4 Replies)
Discussion started by: sushant172
4 Replies

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

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

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

9. Shell Programming and Scripting

extract specific data from xml format file.

Hi, I need to extract the start time value (bold, red font) under the '<LogEvent ID="Timer Start">' tag (black bold) from a file with the following pattern. There are other LogEventIDs listed in the file as well, making it harder for me to extract out the specific start time that I need. . .... (7 Replies)
Discussion started by: 60doses
7 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