Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

xml - empty node

by ambrill (Novice)
on Apr 24, 2013 at 14:03 UTC ( [id://1030411]=perlquestion: print w/replies, xml ) Need Help??

ambrill has asked for the wisdom of the Perl Monks concerning the following question:

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>

Replies are listed 'Best First'.
Re: xml - empty node
by Lotus1 (Vicar) on Apr 24, 2013 at 14:48 UTC

    I already answered this question at your other thread. This node is basically a repost with a broken version of the code I provided. But you didn't give me a chance to respond to the original post.

    Why did you remove the parentheses in the if block? The anonymous poster already pointed out the error. Some basic perl tutorials will help you with syntax.

    When you post questions like these you need to tell us what warnings and errors you get and where you are stuck. Telling us that something doesn't work just means you expect us to do everything to debug "your" code.

    You don't need both the -w after perl and use warnings;

    Why not test your code with several cd/artists in the xml file, some empty and some with values?

Re: xml - empty node
by Anonymous Monk on Apr 24, 2013 at 14:09 UTC

      length() isn't needed here since $foo is the result of $_->to_literal which returns either a string an empty string.

        length() isn't needed here

        sure it is, it clarifies intent

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1030411]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-18 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found