Hi Everyone,
I'm parsing XML data using xml:libxml and am trying to determine the last category based on "category_id" and "parent_id" but can't figure out the logic to do it through hashes or arrays.
The XML looks like this where the last category should be "Carrot"
<products>
<product>
<categories>
<category>
<category_id>443120</category_id>
<name>Vegetables</name>
<parent_id>443026</parent_id>
</category>
<category>
<category_id>445720</category_id>
<name>Carrot</name>
<parent_id>443120</parent_id>
</category>
<category>
<category_id>443026</category_id>
<name>Food</name>
</category>
</categories>
</product>
<products>
I've put it into a hash like this but can't figure out how to loop through the hash and determine the last category.
$VAR1 = {
'Vegetables' => {
'id' => '443120',
'parent_id' => '443026'
},
'Food' => {
'parent_id' => '',
'id' => '443026'
},
'Carrot' => {
'parent_id' => '443120',
'id' => '445720'
}
};
This is my current code
foreach my $part ($dom->findnodes('/products/product')) {
foreach my $categories($part->findnodes('./categories/category'))
+{
$cat_name = $categories->findvalue("name");
$cat_id = $categories->findvalue("category_id");
$cat_parent_id = $categories->findvalue("parent_id");
$catdata{$cat_name} = {"id" => $cat_id, "parent_id" => $cat_pa
+rent_id};
}
}
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.