Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

XML::Smart help, how to access attributes?

by s_pradeep (Initiate)
on Jun 01, 2011 at 12:34 UTC ( #907617=perlquestion: print w/replies, xml ) Need Help??
s_pradeep has asked for the wisdom of the Perl Monks concerning the following question:

for this XML
<feed xmlns='' xmlns:openSearch='' xmlns:gContact='' xmlns:batch='' xmlns:gd='' gd:etag='W/"CUMBRHo_fip7ImA9WxRbGU0."'> <id></id> <updated>2008-12-10T10:04:15.446Z</updated> <category scheme='' term='' /> <title>Elizabeth Bennet's Contacts</title> <link rel='' type='application/atom+xml' href=' +l' /> <link rel='' type='application/atom+xml' href=' +l' /> <link rel='' type='application/atom+xml' href=' +l/batch' /> <link rel='self' type='application/atom+xml' href=' +l?max-results=25' /> <author> <name>Elizabeth Bennet</name> <email></email> </author> <generator version='1.0' uri=''> Contacts </generator> <openSearch:totalResults>1</openSearch:totalResults> <openSearch:startIndex>1</openSearch:startIndex> <openSearch:itemsPerPage>25</openSearch:itemsPerPage> <entry gd:etag='"Qn04eTVSLyp7ImA9WxRbGEUORAQ."'> <id> +12de </id> <updated>2008-12-10T04:45:03.331Z</updated> <app:edited xmlns:app=''>2008-12-10T04:4 +5:03.331Z</app:edited> <category scheme='' term='' /> <title>Fitzwilliam Darcy</title> <gd:name> <gd:fullName>Fitzwilliam Darcy</gd:fullName> </gd:name> <link rel='' type +='image/*' href=' +om/c9012de' gd:etag='"KTlcZWs1bCp7ImBBPV43VUV4LXEZCXERZAc."' /> <link rel='self' type='application/atom+xml' href=' +ull/c9012de' /> <link rel='edit' type='application/atom+xml' href=' +ull/c9012de' /> <gd:phoneNumber rel='' primary='true'> 456 </gd:phoneNumber> <gd:extendedProperty name='pet' value='hamster' /> <gContact:groupMembershipInfo deleted='false' href=' +/270f' /> </entry> </feed>
I tried this,
my $xml_parser = XML::Smart->new($res->content); print $xml_parser->{feed}->{'openSearch:totalResults'},"\n"; foreach my $entry (@{$xml_parser->{feed}->{entry}}) { printf('%s - %s%s',$entry->{'title'},$entry->{'gd:etag'},"\n"); }
How do I access the attribute gd:etag ?

Replies are listed 'Best First'.
Re: XML::Smart help, how to access attributes?
by toolic (Bishop) on Jun 01, 2011 at 12:51 UTC
    I'm not sure what your $res->content is, but if I put your XML into a file named file.xml, I get this expected output:
    use warnings; use strict; use XML::Smart; my $xml_parser = XML::Smart->new('file.xml') ; print $xml_parser->{feed}->{'openSearch:totalResults'},"\n"; for my $entry (@{$xml_parser->{feed}->{entry}}) { printf('%s - %s%s', $entry->{'title'}, $entry->{'gd:etag'}, "\n"); } __END__ 1 Fitzwilliam Darcy - "Qn04eTVSLyp7ImA9WxRbGEUORAQ."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://907617]
Approved by toolic
Front-paged by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2019-02-21 01:17 GMT
Find Nodes?
    Voting Booth?
    I use postfix dereferencing ...

    Results (108 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!