1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/perl -w
use LWP::Simple;
use Data::Dumper;
use XML::Simple qw(:strict);
my $xml = XML::Simple->new;
my $hashref = $xml->XMLin('contact.xml',forcearray => 1);
open (XMLEXPORT, ">test.txt");
print XMLEXPORT Dumper($hashref);
close (XMLEXPORT);
foreach my $content (keys %$hashref) {
if ( ref($hashref->{$content}) eq 'ARRAY' and @{$hashref->{$content}} > 0) {
my @tmp = @{$hashref->{$content}};
foreach my $value (@tmp) {
print $content." => ".$value."\n";
}
} else {
print $content." => ".$hashref->{$content}."\n";
}
}
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
#!/usr/bin/perl use strict; use warnings; use LWP::Simple qw(getstore); use Data::Dumper; use XML::Simple qw(:strict); getstore('http://gdata.youtube.com/feeds/api/users/user/contacts?start-index=1&max-results=50', 'contact.xml'); my $xml = XML::Simple->new; my $hashref = $xml->XMLin('contact.xml',ForceArray => 1, KeyAttr => []); open (XMLEXPORT, ">test.txt"); print XMLEXPORT Dumper($hashref); close (XMLEXPORT); foreach my $content (keys %$hashref) { if ( ref($hashref->{$content}) eq 'ARRAY' and @{$hashref->{$content}} > 0) { my @tmp = @{$hashref->{$content}}; foreach my $value (@tmp) { print $content." => ".$value."\n"; } } else { print $content." => ".$hashref->{$content}."\n"; } }
QuoteAllerdings kriege ich Daten nicht in die Arrays. Zum Beispiel die Daten im Bereich "content".
1
2
3
4
my (@xx_user);
foreach my $key($hashref){
push(@xx_user,$hashref->{$key}->{entry}->{ytusername});
}
2010-04-07T06:30:11 Prelude
for my $key (keys %$hashref)
QuoteAls Antwort erhalte ich dann:
Use of uninitialized value $_ in concatenation (.) or string at kontakte-utube.pl line 52.
2010-04-07T08:08:14 pqhm, da hat aber einer nicht getestet.
wenn man über die keys einer hashref iterieren will, schreibt man:
Code (perl): (dl )for my $key (keys %$hashref)
alles andere wäre voodoo (woher soll perl wissen, dass du hier die keys willst).
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"; }
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' } };
1
2
3
4
<media:title type="plain">
Hier steht ein Titel
</media:title>
<ytduration seconds="636"/>
1
2
3
4
5
for my $entry (@$entrys){ #Video-published ins Array schreiben
if(exists($entry->{'title'}))
{
push(@xx_videotitle,$entry->{'title'});
}
1
2
3
4
5
for my $entry (@$entrys){ #Video-ytduration ins Array schreiben
if(exists($entry->{'ytduration'}->{'seconds'}))
{
push(@xx_videoduration,$entry->{'ytduration'}->{'seconds'});
}