This is still in a fairly early stage of development, but take a look at the module XML::LibXML::Augment which I'm developing. (Not on CPAN yet I'm afraid.)

The idea is that XML::LibXML already provides a nice object-oriented view of the data, but we can specialise each object to make it even better. So say we have some XML:

<employees xmlns="http://megacorp.example/"> <person> <name>Alice</name> <salary>40000</salary> <bank_account number="10001234"/> </person> <person> <name>Bob</name> <salary>37500</salary> <bank_account number="10005678"/> </person> </employees>

Normally the two <person> elements would be XML::LibXML::Element objects. With XML::LibXML::Augment, we create a subclass of XML::LibXML::Element and bind it to the namespace-qualified name {http://megacorp.example/}person. And then whenever we hit a <person> element in our document, our subclass is automatically used.

So we might write:

{ package MegaCorp::Person; use XML::LibXML::Augment -names => ['{http://megacorp.example/}person']; sub get_salary { shift -> getElementsByTagName('salary') -> get_node(1) # not zero-indexed! -> textContent; } sub get_bank_account { shift -> getElementsByTagName('bank_account') -> get_node(1); } sub pay_salary { my $self = shift; my $acct = $self->get_bank_account; $acct->accept_funds($self->get_salary); } } { package MegaCorp::BankAccount; use XML::LibXML::Augment -names => ['{http://megacorp.example/}bank_account']; sub accept_funds { ... } } { package main; use XML::LibXML::Augment; my $doc = XML::LibXML->load_xml(location => 'emp.xml'); # Switch on augmentation for this document! XML::LibXML::Augment->upgrade($doc); my @employees = $doc->getElementsByTagName('person'); $_->pay_salary for @employees; }

If you think you'd like to give it a whirl, do get in touch with me! I'd love to have some extra people playing with it and reporting bugs and ideas for improvements.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: Create OO object from XML by tobyink
in thread Create OO object from XML by Anonymous Monk

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.