| |||||||||||
| |||||||||||
Parsing an XML feed which we get off a web server
Web Service - Our Own Client example from a Well House Consultants training course
More on Web Service - Our Own Client [link]
Source code: xmx Module: P405
#!/usr/bin/env perl
# Parsing an XML feed which we get off a web server $urlsource = "http://www.firstgreatwestern.info/coffeeshop/index.php?type=rss;action=.xml"; # Use the Library for Web Processes, "User Agent" (i.e. browser) module use LWP::UserAgent; # Load from web, or local copy if there's a recent one available if (-e "latest.txt" and -M "latest.txt" > 0.01) { $agent = LWP::UserAgent->new; # Create me a browser $agent->agent("Well House Consultants"); # Set the browser name $req = HTTP::Request->new(GET => ($urlsource)); # Set up the request we'll run $res = $agent->request($req); # Run the request $page = $res -> content(); # Pull back the content! open (FHO,">latest.txt") ; # Save the raw XML (for caching purposes) print FHO $page; close FHO; print STDERR "Data read from server\n"; } else { open (FH,"latest.txt"); read (FH,$page,-s "latest.txt"); close FH; print STDERR "Cached recent copy of data used\n"; } use XML::Parser; # XML Parser # $struct is a pointer to a LIST of tags in the XML document # within a start / end pair, you'll have other lists of tags # attributes are hashes if ($ARGV[0] eq "-tree") { # DOM like $parser = new XML::Parser(Style => "Tree"); $struct = $parser -> parse($page); displaylist("",$struct); } elsif ($ARGV[0] eq "-stream") { # SAX like $parser = new XML::Parser(Style => "Stream"); $parser -> parse($page); } elsif ($ARGV[0] eq "-handle") { # SAX / a bit more selective $parser = new XML::Parser(Handlers => {Start => \&entering, Char => \&handle_char}); $parser -> parse($page); } else { die ("Usage: $0 -[tree|stream|handle]\n"); } ################################################################ sub displaylist { my ($inset,$current) = @_; $inset .= " "; my $k; for ($k=0;$k<@$current;$k++) { if ($type = ref $$current[$k]) { print ("$inset$k: $type\n"); if ($type eq "ARRAY") { displaylist($inset,$$current[$k]); } elsif ($type eq "HASH") { displayhash($inset,$$current[$k]); } } else { my $info = $$current[$k]; $info =~ tr/ -~/!/c; print ("$inset$k: $info\n"); } } } sub displayhash { my ($inset,$current) = @_; $inset .= " "; my $k; foreach $k (sort keys %$current) { if ($type = ref $$current{$k}) { print ("$inset$k: $type\n"); if ($type eq "ARRAY") { displaylist($inset,$$current{$k}); } elsif ($type eq "HASH") { displayhash($inset,$$current{$k}); } } else { my $info = $$current{$k}; $info =~ tr/ -z/!/c; print ("$inset$k: $info\n"); } } } ########################################################################################## sub StartTag { print "Start Tag -"; ppars(@_); } sub EndTag { print "End Tag -"; ppars(@_); } sub Text { print "Text -"; ppars(@_); } sub StartDocument { print "Start Document -"; ppars(@_); } sub EndDocument { print "End Document -"; ppars(@_); } sub ppars { my($obj,$el) = @_; print " Object: $obj "; print "Element: $el\n"; } ################################################################################################### sub entering { # say_pars("tag", @_); # for debug / demo $latest = $_[1]; } sub handle_char { # say_pars("text", @_); # for debug / demo if ($latest eq "guid") { say_pars("url", @_); } if ($latest eq "title") { say_pars("text", @_); } } sub say_pars { my($what,$obj,$el) = @_; return unless ($el =~ /\S+/); print "$what: $el\n"; } __END__ Sample output munchkin:s12perl grahamellis$ perl xmx -handle Cached recent copy of data used text: First Great Western Coffee Shop text: Re: A good problem to have? url: http://www.firstgreatwestern.info/coffeeshop/index.php?topic=11326.msg118375#msg118375 text: Re: Unique opportunity to travel the Portbury Branch Line - Saturday 29 Sept 2012 url: http://www.firstgreatwestern.info/coffeeshop/index.php?topic=11167.msg118374#msg118374 text: Re: A good problem to have? url: http://www.firstgreatwestern.info/coffeeshop/index.php?topic=11326.msg118373#msg118373 text: Re: FGW (bad) experience 27/9/2012 - Maidenhead to Paddington url: http://www.firstgreatwestern.info/coffeeshop/index.php?topic=11318.msg118371#msg118371 text: Re: GWS Steam Railmotor on Looe Valley Line Suns 4 and 11 November url: http://www.firstgreatwestern.info/coffeeshop/index.php?topic=11317.msg118370#msg118370 munchkin:s12perl grahamellis$ Learn about this subject
This module and example are covered on our public Perl Extra course. If you have a group of
three or more trainees who need to learn the subject, we can also arrange a
private or on site course for you.
Books covering this topic
Yes. We have over 700 books in our library. Books
covering Perl are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Web Service - Our Own Client" training module. You'll find a description of the topic and some
other closely related examples on the "Web Service - Our Own Client" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |