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"; }
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 advanceproducts.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>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: XML::LibXML- Escape Empty Tags
by Your Mother (Archbishop) on Jul 30, 2009 at 16:36 UTC | |
by khalistoo (Initiate) on Jul 31, 2009 at 08:46 UTC | |
by Your Mother (Archbishop) on Jul 31, 2009 at 17:31 UTC | |
by Anonymous Monk on Jul 31, 2009 at 09:18 UTC | |
Re: XML::LibXML- Escape Empty Tags
by ramrod (Curate) on Jul 30, 2009 at 14:35 UTC | |
by ikegami (Patriarch) on Jul 30, 2009 at 16:21 UTC | |
by Anonymous Monk on Jul 30, 2009 at 15:21 UTC | |
by ramrod (Curate) on Jul 30, 2009 at 15:58 UTC | |
by khalistoo (Initiate) on Jul 31, 2009 at 08:36 UTC | |
Re: XML::LibXML- Escape Empty Tags
by Anonymous Monk on Jul 31, 2009 at 14:42 UTC |
Back to
Seekers of Perl Wisdom