ambrill has asked for the wisdom of the Perl Monks concerning the following question:
XML file below - alternatives would be if artist "adele" were blank for example.#!/usr/bin/perl -w use strict; use warnings; my $filename = 'library1.xml'; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($filename); print $_->data . " | " foreach ($doc->findnodes('/library/cd/artis +t/text()')); foreach ($doc->findnodes('/library/cd/artist')){ my $artist = $_->to_literal; if $artist { print "$artist | "; else { print "0 | "; } }
<library> <cd> <artist>adele</artist> </cd> <book> <title>Perl Best Practices</title> <author>Damian Conway</author> <isbn>0596001738</isbn> <pages>542</pages> <image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif" width="145" height="190" /> </book> <book> <title>Perl Cookbook, Second Edition</title> <author>Tom Christiansen</author> <author>Nathan Torkington</author> <isbn>0596003137</isbn> <pages>964</pages> <image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gi +f" width="145" height="190" /> </book> <book> <title>Guitar</title> <author>Mark Phillips</author> <author>John Chappell</author> <isbn>076455106X</isbn> <pages>392</pages> <image src="http://media.wiley.com/product_data/coverImage/6X/07 +645510/076455106X.jpg" width="100" height="125" /> </book> </library>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: xml - empty node
by Lotus1 (Vicar) on Apr 24, 2013 at 14:48 UTC | |
|
Re: xml - empty node
by Anonymous Monk on Apr 24, 2013 at 14:09 UTC | |
by Lotus1 (Vicar) on Apr 24, 2013 at 14:50 UTC | |
by Anonymous Monk on Apr 24, 2013 at 14:59 UTC | |
by Lotus1 (Vicar) on Apr 24, 2013 at 15:36 UTC |