Hi All,

It is probably my misunderstanding, your advices are very much appreciated. I made an xml file to show the problem, the real file(s) are much more complex.

<?xml version="1.0" encoding="utf-8"?> <root> <departments> <department> <name>A</name> <articles> <article> <production> <country>France</country> <year>1989</year> </production> <key>001</key> </article> <article> <production> <country>Italy</country> <year>1991</year> </production> <key>002</key> </article> <article> <extra> <production> <country>Germany</country> <year>1995</year> </production> <extrakey>003</extrakey> </extra> </article> </articles> </department> <department> <name>B</name> <articles> <article> <key>004</key> </article> <article> <key>005</key> </article> </articles> </department> <department> <name>C</name> </department> </departments> </root>

I parse the file with XML::Rabbit using the following library file (made for this example as well).

package House { use XML::Rabbit::Root; has_xpath_object_list depts => '/root/departments/department' => 'House::Depts'; finalize_class(); } package House::Depts { use XML::Rabbit; has_xpath_object_list articles => './articles/article[key]|./artic +les/article/extra' => 'House::Article'; has_xpath_value name => './name'; finalize_class(); } package House::Article { use XML::Rabbit; has_xpath_value key => './key|./extrakey'; has_xpath_object prod => './production' => 'Article::Production'; finalize_class(); } package Article::Production { use XML::Rabbit; has_xpath_value country => './country'; has_xpath_value year => './year'; finalize_class(); } 1;

Now I use this library file in the following script.

#!/perl use strict; use warnings FATAL => qw(all); use Encode; use FindBin qw($Bin); use lib $Bin; use testlib; use Try::Tiny; my $path = shift or die "No source!\n"; my $string = do { local $/ = undef; open my $in, "<", "$path" or die "$!";<$in>;}; my $xml = decode('utf8', $string); my $h = House->new(xml => $xml); for my $d ( @{$h->depts} ) { for my $art ( @{$d->articles} ) { my $prod = try {$art->prod} catch {0}; # $art->prod // 0; my $country = $prod ? $prod->country : ''; print join("\t", $d->name, $art->key, $country), $/; } }

You see that not all the articles have the "production" child. The line my $prod = $art->prod//0; (commented) would produce an error: "Attribute (prod) does not pass the type constraint because: Validation failed for 'Article::Production' with value undef at reader House::Article::prod (unknown origin)..." If I use try {...} catch {...}, then I get the awaited output.

A 001 France A 002 Italy A 003 Germany B 004 B 005

Now two questions.

Thank you very much in advance.


In reply to Parsing XML with XML::Rabbit - two questions by vagabonding electron

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.