473,378 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to read a value from an XML tag

I have the following XML file and am able to read the entire file and all tags.

The problem is how to read the main value \
there is only one contact with main=true.

How do I the value of main from the tag <Contact main="true">
Expand|Select|Wrap|Line Numbers
  1. <Contacts>
  2.          <Contact main="true">
  3.                <Firstname>Sandy</Firstname>     
  4.                <Email>sandy@abc.com</Email>
  5.         </Contact>
  6.         <Contact main="false">
  7.                <Firstname>Sandra</Firstname>     
  8.                <Email>sandra@abc.com</Email>
  9.         </Contact>
  10.         <Contact main="false">
  11.                <Firstname>Peter</Firstname>     
  12.                <Email>peter@abc.com</Email>
  13.         </Contact>
  14.  
  15. </Contacts>
This is the code snippet:

Expand|Select|Wrap|Line Numbers
  1. my $contacts_node = $root->child("Contacts");
  2.  
  3.     my @contact_list = $contacts_node->child("Contact");
  4.  
  5.     # extract the contact from the contacts collection
  6.     foreach my $contact_node (@contact_list) {
  7.         foreach my $contact ($contact_node) {
  8.             my $first_name_node = $contact->child("Firstname");
  9.     }
  10. }
May 21 '10 #1
2 3729
toolic
70 Expert
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use XML::Twig;
  4.  
  5. my $xml = <<'XML';
  6. <Contacts> 
  7.          <Contact main="true"> 
  8.                <Firstname>Sandy</Firstname>      
  9.                <Email>sandy@abc.com</Email> 
  10.         </Contact> 
  11.         <Contact main="false"> 
  12.                <Firstname>Sandra</Firstname>      
  13.                <Email>sandra@abc.com</Email> 
  14.         </Contact> 
  15.         <Contact main="false"> 
  16.                <Firstname>Peter</Firstname>      
  17.                <Email>peter@abc.com</Email> 
  18.         </Contact> 
  19.  
  20. </Contacts> 
  21. XML
  22.  
  23. my $t = XML::Twig->new();
  24. $t->parse($xml);
  25.  
  26. for my $cont ($t->root()->children('Contact')) {
  27.     print $cont->att('main'), ' ', 
  28.           $cont->first_child('Firstname')->text(), "\n";
  29. }
  30.  
  31. __END__
  32.  
  33. true Sandy
  34. false Sandra
  35. false Peter
  36.  
May 22 '10 #2
Hi,

I have got the solution to it, the keyword attribute is used to read the attribute from an XML tag:
Expand|Select|Wrap|Line Numbers
  1. my $contact_main_attribute = $contact_node->attribute("main");
  2.  
Thanks a lot for the answer, however I am using XML::Simple I should have mentioned it earlier.

This is the complete solution to read the XML given in the question alowg with the attributes using XML::Simple.

Expand|Select|Wrap|Line Numbers
  1.  my $contacts_node = $root->child("Contacts");
  2.  
  3.  my @contact_list = $contacts_node->child("Contact");
  4.  
  5.  # extract the contact from the contacts collection
  6.     foreach my $contact_node (@contact_list) {
  7.          my $contact_main_attribute = $contact_node->attribute("main");
  8.          foreach my $contact ($contact_node) {
  9.              my $first_name_node = $contact->child("Firstname");
  10.        }
  11.    }
  12.  
  13.  
Regards,
Sakshi
May 27 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Krechting | last post by:
Hi All, I tried to read the value of a textbox in another form. I have a page which contains one record. The textbox "txtWID" contains the autonumber of the record. I want to read this value in...
3
by: SzokU | last post by:
Hi All! I have problem... I want to write/read value from param tag in applet, but I don't know how... This is exaple code:...
1
by: Dan | last post by:
This is one that has me stumped and I need an expert's input. Any ideas why the values from the second script-generated drop down list isn't recognized by the script to add time values to the...
3
by: laskowski | last post by:
How can I determine the value TEST from both of the scenarios below? For the <P> tag I can use OMON_3.innerText, but with the <Input> tag I need to use OMON_3.value. However, in my application...
1
by: myahact | last post by:
Hello, I have <input type=file id=myfile> in a form. In IE I can read the value thusly: document.getElementById("myfile").value But thusly returns "" in Mozilla/Netscape. I also tried...
7
by: Gene | last post by:
I have a number of aspx pages on which a single user control appears. All of the aspx pages and the user control make user of code-behind modules. I need for logic in the user control's code-behind...
2
by: zoneal | last post by:
Hi everybody there .....I need help ..... I want know how I can get or read value from website by using Visaul basic Net .....Is this possible or not .... I want to get value from this website...
1
by: Winpygsfli | last post by:
I have an application that has an iFrame wich houses a form. I need to read the value of a specific input field from the iFrame. iFrame - portalArea Form - frmUserProfile Input field -...
4
by: Michael Munch | last post by:
Hi I want to read the value of af text-field, create dynamic, in a form. Se below a small test-site to do that (but readning fails): I use the function Test_Read for reading the value from the...
1
by: frans1 | last post by:
Hi, To auto populate one of our data bases, I need to be able to read content from an Excel sheet using Access (2003) vba. Obtaining the content from a standard cell ( A1 , C3 , etc) is easy ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.