Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Create OO object from XML
by kcott (Archbishop) on Mar 22, 2012 at 02:42 UTC | |
|
Re: Create OO object from XML
by GrandFather (Saint) on Mar 22, 2012 at 02:38 UTC | |
|
Re: Create OO object from XML
by Jenda (Abbot) on Mar 22, 2012 at 08:35 UTC | |
|
Re: Create OO object from XML
by jeffa (Bishop) on Mar 22, 2012 at 16:00 UTC | |
|
Re: Create OO object from XML
by tobyink (Canon) on Mar 22, 2012 at 09:09 UTC |