How To get the data from a tag in XML File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How To get the data from a tag in XML File
# 1  
Old 12-03-2008
How To get the data from a tag in XML File

Hi
I have a XML file in which data is loaded from a relational table and the column names are tags in the xml file which is shown below.

<State>UN</State><Zip/><CompanyName/><EmailAddress>FDF@gmail.COM</EmailAddress><PromoType>UNKNOWN</PromoType></Promotion></PromotionList<State>UN</State><Zip/><CompanyName/><EmailAddress>zd4946@gmail.com</EmailAddress>

what I have to do is : have to check the data in between tags which are in bold is valid or not ... means have to check whether its a email address or not ... to check this i need to view the data in between those tags.

and have to find the length of the data which is in between tag...means length of FDF@gmail.COM

for this I need to get the data from the xml file whereever <EmailAddress></EmailAddress> tag is present.



sorry if its already asked...i checked but i didnt get Exatly matching result for my requirement

any help in this...Im doing this in korn shell

Last edited by naughty21; 12-04-2008 at 01:51 PM..
# 2  
Old 12-03-2008
Hi,

i would try to extract the emails directly. For me this works with your sample:

Code:
email=($(grep -o "[0-9A-Za-z]\+@[0-9A-Za-z]\+\.[A-Za-z]\{2,3\}" file))

This writes all patterns, and only these patterns, matching the regexp
into the array email.

Code:
echo ${#email[0]}

Will give you the length of the first element. Without the "#" it will give
you the entry at position 0.

HTH Chris
# 3  
Old 12-03-2008
chris thanks for your reply


but
when im trying with below one

Code:
email=($(grep -o "[0-9A-Za-z]\+@[0-9A-Za-z]\+\.[A-Za-z]\{2,3\}" 456))

im getting this error 

ksh: 0403-057 Syntax error: `(' is not expected.



when im trying with below one
Code:
email=$(grep -o "[0-9A-Za-z]\+@[0-9A-Za-z]\+\.[A-Za-z]\{2,3\}" 456)

im getting error like this 

grep: Not a recognized flag: o
Usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwy] [-p[parasep]] -e pattern_list...
        [-f pattern_file...] [file...]
Usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwy] [-p[parasep]] [-e pattern_list...]
        -f pattern_file... [file...]
Usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwy] [-p[parasep]] pattern_list [file...]

any other sggestions
# 4  
Old 12-03-2008
No surprise, you are using ksh. This solution only works in bash or zsh.
With ksh i can't help you. But this should be easy. Just google for arrays
in ksh. $(...) execute the following command in a subshell. (...) puts
the things inside into an array. Perhaps you can replace (...) by
`...` (backticks). But i don't know.

Probably you will have to adjust the regexp, too. By now it will not
match emails with dots, underscores,dashes etc.
# 5  
Old 12-03-2008
Quote:
Originally Posted by Christoph Spohr
No surprise, you are using ksh. This solution only works in bash or zsh.
With ksh i can't help you. But this should be easy. Just google for arrays
in ksh. $(...) execute the following command in a subshell. (...) puts
the things inside into an array. Perhaps you can replace (...) by
`...` (backticks). But i don't know.

Probably you will have to adjust the regexp, too. By now it will not
match emails with dots, underscores,dashes etc.
I got the answer but works with 1st occurance of the tag only

Code:
awk -F '</?EmailAddress>' '{print $2}' 456.xml

but i need for multiple times .... means email address tag exists for multiple times in the file ...
so need to check whole xml file for email address and get them wherever <EmailAddress></EmailAddress> tag is present.

Last edited by naughty21; 12-03-2008 at 05:07 PM..
# 6  
Old 12-04-2008
Re : write values of XML tags into arrays

Hello All,

Hope all is fine. I am using Bourne Shell (sh) . I have this simple XML structure ( it's very well defined and this is how this fixed structure will be). The exact sample is as follows (There will always be one value per tag):

<Users>
<Host>
<hostAddress>180.144.226.47</hostAddress>
<userName>pwdfe</userName>
<password>hjitre</password>
<instanceCount>2</instanceCount>
</Host>
<Host>
<hostAddress>180.144.226.87</hostAddress>
<userName>trrrer</userName>
<password>jhjjhhj</password>
<instanceCount>3</instanceCount>
</Host>
<Host>
<hostAddress>180.455.226.87</hostAddress>
<userName>wewqw</userName>
<password>dfsdfd</password>
<instanceCount>3</instanceCount>
</Host>
</Users>
----------------------------------------------------------------------

Now I want to create an array with only the values of the xml tags . For e.g. H_ARRAY ('180.144.226.47','180.144.226.87','180.144.226.87'). Then I will traverse throught the values of array accordingly. I am newbie to shell scripting and especially "SED" command which after repeated attempts was unable to understand. Would appreciate your help. Let me know if I missed on something,


H_ARRAY=( `echo ${hostAddress}` )
U_ARRAY=( `echo ${userName}` )
P_ARRAY=( `echo ${password}` )
I_ARRAY=( `echo ${instanceCount}` )

Thanks,
Sam
# 7  
Old 12-04-2008
Try this quick approch:
Code:
sed 's/>/\n>/g' filename | sed 's/>\([A-Za-z0-9]*@[A-Za-z0-9]*\.[A-Za-z0-9]*\)<.*/\1/' | sed '/@/!d'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies

2. Shell Programming and Scripting

Moving XML tag/contents after specific XML tag within same file

Hi Forum. I have an XML file with the following requirement to move the <AdditionalAccountHolders> tag and its content right after the <accountHolderName> tag within the same file but I'm not sure how to accomplish this through a Unix script. Any feedback will be greatly appreciated. ... (19 Replies)
Discussion started by: pchang
19 Replies

3. Shell Programming and Scripting

Update particular tag in a XML file

Below is the content in my XML file <name>XXX</name> <eventType>Uptime</eventType> <eventType>Delay</eventType> <eventType>Delay</eventType> <name>YYY</name> <eventType>Uptime</eventType> <eventType>Delay</eventType> ... (12 Replies)
Discussion started by: Viswanatheee55
12 Replies

4. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

5. UNIX for Advanced & Expert Users

Shell Script to read XML tags and the data within that tag

Hi unix Gurus, I am really new to Unix Scripting. Please help me to create a shell script which reads the xml file and from that i need to fetch a particular information. For example <SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME... (2 Replies)
Discussion started by: SmilePlease
2 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

How to retrieve the value from XML tag whose end tag is in next line

Hi All, Find the following code: <Universal>D38x82j1JJ </Universal> I want to retrieve the value of <Universal> tag as below: Please help me. (3 Replies)
Discussion started by: mjavalkar
3 Replies

8. Shell Programming and Scripting

Substitute partial data only within a xml tag

Hello, I have huge xml files and I need to replace only part of the data within a particular xml tag. This doesnt seem to be as simple as it sounds. I have searched everywhere and couldnt find any solution. Ex: In the below case I would like "def" to be replaced by "xyz" only when found in... (8 Replies)
Discussion started by: roshanjain2
8 Replies

9. Shell Programming and Scripting

XML tag replacement from different XML file

We have 2 XML file 1. ORIGINAL.xml file and 2. ATTRIBUTE.xml files, In the ORIGINAL.xml we need some modification as <resourceCode>431048</resourceCode>under <item type="Manufactured"> tag - we need to grab the 431048 value from tag and pass it to database table in unix shell script to find the... (0 Replies)
Discussion started by: balrajg
0 Replies
Login or Register to Ask a Question