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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |