Thread XML::Simple - Weiterverarbeitung der Daten
(13 answers)
Opened by Prelude at 2010-04-06 13:36
Ich verstehe nicht warum man "ForceArray" auf alles setzen sollte. Es reicht doch für "entry" und "link".
Du scheinst die Struktur nicht gaz verstanden zu haben. der Wert zum Schlüssel "entry" ist ein Array. Es ist nicht so, dass alles Hashes sind. Das erzwingst du ja gerade mit "ForceArray=>1" für (fast) alle Elemente. Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #!/usr/bin/perl use strict; use warnings; use LWP::Simple qw(get); use Data::Dumper; use XML::Simple qw(:strict); my @xx_user=(); my $xmldata=get('http://gdata.youtube.com/feeds/api/users/user/contacts?start-index=1&max-results=50'); my $xml = XML::Simple->new(ForceArray => ['entry', 'link'], KeyAttr => []); my $xmlref = $xml->XMLin($xmldata); my $entrys=$xmlref->{entry} or die("NO ENTRYS"); die("NO ARRAY") if(ref($entrys) ne 'ARRAY'); for my $entry (@$entrys) { if(exists($entry->{'yt:status'})) { push(@xx_user,$entry->{'yt:status'}); } } for(@xx_user) { print "$_\n"; } Zur Übesicht über den den XML-Baum: Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 $xmlref={ 'link' => [ { 'rel' => 'alternate', 'href' => 'http://www.youtube.com/profile_friends?user=user', 'type' => 'text/html' }, # ... # ... # ... # ... ], 'entry' => [ { 'yt:status' => 'accepted', 'link' => [ { 'rel' => 'related', 'href' => 'http://gdata.youtube.com/feeds/api/users/clarknet', 'type' => 'application/atom+xml' }, #... #... #... #... ], 'author' => { 'name' => 'user', 'uri' => 'http://gdata.youtube.com/feeds/api/users/user' }, 'published' => '2007-03-07T07:35:03.000-08:00', 'yt:username' => 'clarknet', 'id' => 'http://gdata.youtube.com/feeds/api/users/user/contacts/clarknet', 'updated' => '2010-04-07T08:10:04.931Z', 'category' => { 'scheme' => 'http://schemas.google.com/g/2005#kind', 'term' => 'http://gdata.youtube.com/schemas/2007#friend' }, 'title' => { 'content' => 'clarknet', 'type' => 'text' }, }, #.... #.... #.... #.... #.... ], 'openSearch:startIndex' => '1', 'openSearch:itemsPerPage' => '50', 'author' => { 'name' => 'user', 'uri' => 'http://gdata.youtube.com/feeds/api/users/user' }, 'generator'=> { 'version' => '2.0', 'content' => 'YouTube data API', 'uri' => 'http://gdata.youtube.com/' }, 'logo' => 'http://www.youtube.com/img/pic_youtubelogo_123x63.gif', 'xmlns:yt' => 'http://gdata.youtube.com/schemas/2007', 'openSearch:totalResults' => '603', 'id' => 'http://gdata.youtube.com/feeds/api/users/user/contacts', 'updated' => '2010-04-07T08:10:04.933Z', 'category' => { 'scheme' => 'http://schemas.google.com/g/2005#kind', 'term' => 'http://gdata.youtube.com/schemas/2007#friend' } 'title' => { 'content' => 'Contacts of user', 'type' => 'text' } }; |