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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.