http://www.perlmonks.org?node_id=784625

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

Hi Monks, I am currently parsing an XML file with a script using XML::LibXML. so far i ve been able to get the data outputed as i wanted to, but the script escapes when it reaches an empty tag. Now, i am a pure noob when it comes to perl (and quite a lot of other stuff lol) but I ve tried to read few articles, posts, forums... and as far as i can get i need to test if a node contains data, which i am suppose to do with hasChildNode function. Fair enough, I just don t understand where to use, or how... Here is a piece of code and an XML file
Parsing.pl sub parse{ my $parser = XML::LibXML->new(); my $tree = $parser->parse_file($file); open (my $FhResultat, '>', $FichierResultat ); my $root = $tree->getDocumentElement; my @productname = $root->getElementsByTagName('product'); foreach my $child (@productname){ print {$FhResultat} $child->getElementsByTagName('name')->[0]->getFirstChild->getD +ata, "\t", $child->getAttribute('category_id'),"\t", $child->getAttribute('id'), "\t", $child->getElementsByTagName('desc_short')->[0]->getFirstChild +->getData, "\n"; }
products.xml <product category_id="13296" id="675936193" catalog="false" row="1"> <name>Children's Hand Rake</name> <imageURL_med></imageURL_med> <desc_short>Mini gardeners can dig, rake and scoop out the +ir own plot with this children's hand rake, complete with contoured h +andles and durable metal heads.</desc_short> </product>
Since a normal product will have information in <imageURL_med></imageURL_med> i would like to know how i can code my script to fetch data inside this tag, and return null if there is no data and having the script not stopping when it encounters an empty tag. Thanks a lot in advance