I am trying to retrieve data from various nodes in an xml file. Unfortunately, there are times when the nodes have no values so i am trying to format the data to return a "0" or some character so that the output is consistent with files that do have values or text.
thanks
#!/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 | ";
}
}
XML file below - alternatives would be if artist "adele" were blank for example.
<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>