> -----Message d'origine----- > De : Todd W [mailto:[EMAIL PROTECTED] > Envoyé : mardi 4 janvier 2005 18:49 > À : beginners@perl.org > Objet : Re: xml > > > "Brent Clark" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > Hi > > > > I have been trying to get my perl to work with an xml doc (about 2 hours > > new to xml). > > I basically just want all the <title>elements > > > > would anyone be so kind as too share a link of doc etc as how I can > > takle this. > > > > Kind Regards and thanks in advance > > Brent Clark > > > > ===================================================== > > my $file = get("http://currencysource.com/RSS/ZAR.xml"); > > > > foreach my $element ($file){ > > if ($element =~ /title/i){ > > print "$element<BR>"; > > } > > } > > Piece of cake whith XML::XPath: > > [EMAIL PROTECTED] misc]$ cat parserss.pl > use warnings; > use strict; > > use LWP::Simple; > use XML::XPath; > > my $url = 'http://currencysource.com/RSS/ZAR.xml'; > my $xml = LWP::Simple::get( $url ); > > my $xp = XML::XPath->new( xml => $xml ); > > my $nodeset = $xp->find( '/rss/channel/item' ); > foreach my $node ( $nodeset->get_nodelist ) { > print $xp->findvalue( './title', $node ), "\n"; > } > [EMAIL PROTECTED] misc]$ perl parserss.pl > 1 ZAR = AED (0.649063) > 1 ZAR = ARS (0.523159) > 1 ZAR = AUD (0.226934) > 1 ZAR = BHD (0.066453) > ... > > How cool is that? >
Yes it's cool !, but :) Some issues may require caution: 1) you have to know in advance how the XML file is strutured to pass this info ("/rss/channel/item" in this case) to Xpath. 2) you may need to validate the XML before processing it. In that case an additional file beeing DTD or "XML Schema" will be necessary. Regards, José. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>