By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,372 Members | 1,950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,372 IT Pros & Developers. It's quick & easy.

How to read a value from an XML tag

P: 5
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
Share this Question
Share on Google+
2 Replies


Expert
P: 70
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

P: 5
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

Post your reply

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