Hello Monks,

I am writing some OO classes whose properties will be set based on the contents of XML files. When creating a new instance of the OO class, the user passes an $id. Based on the value of $id, a certain XML file will need to be parsed and the properties of the new instance need to be set from the XML file.

I'm using XML::Parser to do the parsing. How can I set the properties of the new $self object out of the startElement sub that is doing the parsing? Here is what I mean:

package Person; use XML::Parser; sub new { my $class = shift; my $id = shift; my $self = { id => $id }; bless $self; ## now, set up XML::Parser; $parser->parsefile ("/path/to/some/file/$id.xml"); return $self; } # standard XML::Parser stuff here sub startElement { my( $parseinst, $element, %attrs ) = @_; if ($element eq "TAG") { my $property = $attrs{Property}; ## HOW TO SET $self->{property} from here? } }

Or is XML::Parser the wrong tool for this job?

Thanks!


In reply to 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.