Forum Moderators: coopster & phranque

Message Too Old, No Replies

read xml from url

pick sml up

         

solow

9:58 am on Nov 10, 2008 (gmt 0)

10+ Year Member



hello,

I have been working with xml, and svn *codebase* for 2 projects now, but never really used url's for picking up xml files.

Now I found this script, that doesn't work:

[perl]#!/usr/bin/perl -w
use strict;
use CGI;
use XML::Simple;

my $xml = new XML::Simple;

my $xmlData = $xml->XMLin("someurl");

print "$xmlData->{code} name is $xmlData->{name} countryname is $xmlData->{CountryName} section\n";[/perl]

So basically I want to retreive a xml document from a url, and then write it away later, so right now all I need is to have a variable, which can show me the elements of the xml file.

hope you can help me.

greetings.

solow

11:22 am on Nov 10, 2008 (gmt 0)

10+ Year Member



whoops, now I made another script, which does kind of... the right thing, just doesnt show me want I want to see:

[perl]#!/usr/bin/perl -w
use strict;
use CGI;
use LWP::Simple;
use XML::TreeBuilder;
my $url = "http://maps.google.com/maps/geo?q=";
my $google_code = "ABQIAAAAdyFdLejQuxLR-DS7Xwf_ghTToTrERikbexcU9K8fttM4HbfNkBTdvrL8Yjz6FdJPefFm8jXo852aDA";

for(my $pc=11114; $pc < 11115; $pc++){

for(my $pl=80; $pl < 82; $pl++){
for(my $pl1=80; $pl1 < 82; $pl1++){

print retreiveXml(dirtyNumberTrick($pc) . chr($pl) . chr($pl1), $url, $google_code) . "\n";
}
}

}

sub dirtyNumberTrick{
my($value) = @_;
return substr($value,1);
}

sub retreiveXml{
my($pc, $url, $google_code) = @_;
my $link = "$url$pc,NL&output=xml&key=$google_code";
my $xmlPage = get $link;
extractDataXml("$xmlPage");
}

sub extractDataXml{
my ($dataToUse) = @_;
my $tree = XML::TreeBuilder->new();
$tree->parse($dataToUse);
print $dataToUse;
foreach my $postalCodeData ($tree->find_by_tag_name('Response')){
my $blie = $postalCodeData->find_by_tag_name('name');
print $blie;


}

}

[/perl]

This is supposed to show the <name>1032,ER</name>

error comes up,

not well-formed (invalid token) at line 9, column 25, byte 243 at C:/Perl/lib/XML/Parser.pm line 187

heeelp please, this is annoying the crap out of me :p

IanKelley

8:59 pm on Nov 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It looks like the module is complaining about the specific XML you're trying to parse? Since you can't control the feed there's nothing you can do unless there's a flag you can use to change the parser's perspective on what constitutes valid XML.

I generally use regular expressions to parse XML. A one line regular expression is usually all you need to parse a given feed, which is vastly more efficient and gets around picky parser issues.

[edit: typo]

[edited by: IanKelley at 9:03 pm (utc) on Nov. 11, 2008]

solow

8:12 am on Nov 12, 2008 (gmt 0)

10+ Year Member



Okay I got it working,

I had to use xml simple, then I killed the first, and last tag of the xml,

and then the format was okay.

:)