Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: perl & XML: getting last child?

by space_monk (Chaplain)
on Apr 17, 2013 at 20:48 UTC ( #1029225=note: print w/ replies, xml ) Need Help??


in reply to perl & XML: getting last child?

Another way ... :-)

#!/usr/bin/perl -w use strict; use warnings; my $filename = 'library.xml'; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($filename); print "\nBooks\n"; my @nodes = $doc->findnodes("/library/book[last()]"); foreach my $node (@nodes) { print $node->findvalue( 'title')."\n"; my @authors = $node->findnodes('author'); print "Authors:".scalar(@authors)."\n"; foreach my $auth (@authors) { print $auth->textContent()."\n"; } }
If any of my proposed solutions have minor errors, it's because I don't waste my genius on trivial matters. :-P


Comment on Re: perl & XML: getting last child?
Download Code
Re^2: perl & XML: getting last child?
by ambrill (Novice) on Apr 18, 2013 at 12:23 UTC
    This works well. In some cases, the last node is null (or does not exist). How can you print a "0" in those cases? thanks
      If you put some example XML data in a comment, I (or someone else) will almost certaintly work out a way to do it. :-)
      If any of my proposed solutions have minor errors, it's because I don't waste my genius on trivial matters. :-P

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-07-11 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (224 votes), past polls