Locating correct XML tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Locating correct XML tags
# 1  
Old 08-14-2006
Question Locating correct XML tags

hi all, I have a question for you all. I have a xml file that contains 2 similar tags, like the example below,
<some tag>
<Tag ABC=1>
<tag_value1>some_value</tag_value1>
</Tag>
</some tag>
...
<Tag ABC=1>
<tag_value1>some_value</tag_value1>
</Tag>

My question would be, how do you retrieve the second <Tag ABC=1>, and then update the some_value right between the <tag_value1>?

I had created following scripts (a section of the whole scripts)
--------------------------------------------------------------------------
cat (file.xml) | while read data
newValue="<tag_value1>some_other_value</tag_value1>"
xmlHelper="echo $data"
echo "$xmlHelper" > $tempXML
strHelper="<Tag ABC=1>\n <tag_value1>some_value</tag_value1>"
oldValue=$(awk -F 'BEGIN{FS="<tag_value1>|</tag_value1>"}
/myMatch/{
for(i=1;i<=NF;i++) {
if [[ ${xmlHelper} = ${strHelper} ]]
then
print "<tag_value1>\(.*)</tag_value1>"
}
}
END {print $2}' file.xml)

if [[ ${xmlHelper} = ${strHelper} ]]; then
sed -e 's/$oldValue/newValue' file.xml
--------------------------------------------------------------------------

I am using ksh to do all these, the problem is that it would not run. Kept giving me an error of awk at line 2. Tried to go around but it come back the same way.

Wonder if anyone have a good suggestion?
# 2  
Old 08-15-2006
Tou can try something like this (not tested) :
Code:
awk '
   /<TAG ABC=1>/ { 
       tagnum += 1 
   }
   /<tag_value1>/ && tagnum==2 { 
       print "<tag_value1>some_other_value</tag_value1>" ; 
       next 
   }
   1
' file.xml


Jean-Pierre.
# 3  
Old 08-15-2006
well i had tried your section and replaced it with mine it does not work though giving me an error of,

awk: syntax error near line 10
awk: bailing out near line 10

But I had took out the number "1" it works fine now, still have a couple of errors to fix but i think i will be able to manage it, thank you for your time, aigles.
# 4  
Old 08-15-2006
The number 1 alone in the awk script is the same thing as
Code:
{ print $0 }


Jean-Pierre.
# 5  
Old 08-15-2006
ah ok... thank you. But the version of this awk in the ksh that i am using does not support that though. What I had done is as follow
awk '
/<TAG ABC=1>/ {
tagnum += 1
}
/<tag_value1>/ && tagnum==2 {
print $0;
next
}
' file.xml

Thank you for your help though, it had pushed me toward the right direction.
# 6  
Old 08-15-2006
if on Solaris, try the original aigles' code with '/usr/bin/nawk' OR '/usr/xpg4/bin/awk' instead of thein old 'awk'
# 7  
Old 08-15-2006
Nope i am running this on SunOS Generic_117350-33 sun4u sparc SUNW,Sun-Fire

well i am still fixing aigles' code to suite my need, because his codes will return all the related value, but all i want is just the correspondent value

i.e.
<some tag>
<Tag ABC = 1>
<tag_value1>some_value</tag_value1>
</Tag>
<some tag>
...
<Tag ABC = 1>
<tag_value1>some_value</tag_value1>
</Tag>

his scripts will return each and every instances that match <tag_value1> and the second condition will always be true, not really what I wanted, but extremely close (thank you aigles). I just wanted to extract the second

<Tag ABC = 1>
<tag_value1>some_value</tag_value1>
</Tag>

and the change the "some_value" to a new value.

Last edited by ahtat99; 08-15-2006 at 12:29 PM..
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. Solaris

Locating files!

My question is : -How do I locate files of specific user in specific folder. In other words, How do I find files that belong only to "root" user, in /etc folder? (5 Replies)
Discussion started by: alexex666
5 Replies

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

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

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

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

7. Shell Programming and Scripting

Data between XML Tags

<?xml version="1.0" encoding="iso-8859-1" ?> <TABLE> <TEST> <ID> 123 </ID> <name> abc </name> </TEST> <TEST> <ID> 123 </ID> <name> abc2 </name> </TEST> </TABLE> <TABLE> <TEST> <ID> 456 </ID> <name> def </name> </TEST> <TEST> ... (8 Replies)
Discussion started by: eskay
8 Replies

8. UNIX for Advanced & Expert Users

Search in xml tags

I have a xml file like below. I want to search in <msg>, if found corresponding tag path needs to fetch. for eg: for the message 'RM1659 Boeing Exception Management' it should return /trunk/talend/source/SITA_SP7/Job_Designs/DeferredOutputs/SmartViewFiles/DO_Smartview.zip For message it can... (2 Replies)
Discussion started by: svenkatareddy
2 Replies

9. Shell Programming and Scripting

Why are the xml tags not visible?

Could some one please tell me why, when I run the following php code: <HTML> <HEAD> <TITLE></TITLE> </HEAD> <BODY> <?php $readfile = file("rss_file.xml"); for ($k=0; $k<=count($readfile)-1; $k++) { echo "$readfile<br>"; } </BODY> </HTML> the tags do not appear in... (1 Reply)
Discussion started by: photon
1 Replies

10. Shell Programming and Scripting

Grep xml tags

Hi I want to get the value between to XML tags as follows <EAN>12345</EAN> so i would want to return 12345. i have tried sed and awk but can't do it. can anyone help? (9 Replies)
Discussion started by: handak9
9 Replies
Login or Register to Ask a Question