Re: While Parsing through XML file using SAX, How can I create object of module which is equivalent to element value ?

Tod Harter <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
Well, there are various possibilities. Presumably you're going to want to
accumulate some amount of data from the child elements of your tag. My
approach would be to create an object in the START tag and then assign its
properties as you go, when you hit the end tag do whatever with it.
Depending on how fancy you need to be and what the object graph you're going
to produce is would dictate the exact code structure.

I guess I'm not sure what the problem you're running into is.

On Wed, Nov 18, 2009 at 11:51 AM, [email protected] <[email protected]>wrote:

> I have different modules like Author.pm, BillingPeriod.pm,
> Offer.pm,PaymentMethod.pm etc. now in sax whenever I hit the end element tag
> I want to create object of module which is equivalent to element value. How
> can I achieve this ? For example if am parsing through XML file and sax
> parser hit's end element as </offer> than it should create object of
> Offer.pm, similarly if sax parser hit's end element tag as </author> than it
> should create object of Author.pm Code XML: `books.xml` <?xml version="1.0"
> encoding="UTF-8"?> <!--Sample XML file generated by XMLSpy v2009 sp1 (
> http://www.altova.com)--> <bks:books xsi:schemaLocation="urn:books
> Untitled1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:bks="urn:books"> <book id="String"> <author>String</author>
> <authorFirstName>String</authorFirstName>
> <authorLastName>String</authorLastName> <title>String</title>
> <titleNo>3</titleNo> <genre>String</genre> <offer>String</offer>
> <price>3.14159E0</price> <pub_date>1967-08-13</pub_date>
> <review>String</review> <reviewsratings></reviewratings> </book>
> </bks:books> sax: `perlsaxparsing.pl` #!usr/bin/perl -w use
> XML::SAX::ParserFactory; use MySaxHandler; my $handler =
> MySaxHandler->new(); my $parser = XML::SAX::ParserFactory->parser(Handler =>
> $handler); $parser->parse_uri("books.xml") For example in below example,
> assuming that sax is hitting `Price end element tag` so am creating object
> of `Price.pm` SAX Parser Handler Module: `MySaxHander.pm` sub end_element {
> my($self,$data) = @_; print "\t Ending element:".$data->{Name}."\n"; my $obj
> = new Price("1","2","NL","ENUM","DESCRIPTION","2008-01-01
> 10:00:00","2009-01-01 10:00:00","2008-01-01 10:00:00","USER"); print
> $obj->getSQLScript."\n"; $in_books--; } **Question:** While Parsing through
> XML file using SAX, How can I create object of module which is equivalent to
> element value ?
> _______________________________________________
> Perl-XML mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
>


-- 
The Wise adapts himself to the world. The Fool adapts the world to himself.
Therefore, all progress depends on the Fool.

_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.