XML::Parser Style => Object

M

Mark J Fenbers

Does anyone have some sample code I could examine to learn how to use the
XML::parser package when Style is set to Objects? E.g.,

my $p = new XML::parser(Style => Objects");
$p-Parsefile("myfile.xml");
# now what?

I'm still pretty green at Perl, and maybe "now what?" is obvious to more
experienced Perl coders, but not me...

Here is an excerpt of "myfile.xml" which I'd like to be able to parse and make
use of the data somehow in my Perl code...

<?xml version="1.0 ?>
<site id="ZLPP1">
<sigstages>
<action>8.0</action>
<flood>11.0</flood>
<moderate>13.0</moderate>
<major>17.0</major>
<record>19.4</record>
</sigstages>
<observed>
<datum>
<stage units="feet">3.16</stage>
<valid timezone="EST">2004/02/14 02:15</valid>
</datum>
<datum>
<stage units="feet">3.10</stage>
<valid timezone="EST">2004/02/14 02:45</valid>
</datum>
</observed>
</site>
 
M

Mark J Fenbers

Yes, XML::Simple works great for me! Thanks for the tip!
Mark

Michel said:
Mark said:
Does anyone have some sample code I could examine to learn how to use the
XML::parser package when Style is set to Objects? E.g.,
[...]
I'm still pretty green at Perl, and maybe "now what?" is obvious to more
experienced Perl coders, but not me...
[...]

Is there any specific reason why you chose to use XML::parser?
XML::parser is really a low level module, that IMHO should not be used
directly.

I think XML::Simple would work just fine in your case. It will read the
XML into a Perl data structure. Or try XML::LibXML, or XML::Twig if you
need more features. Look at the FAQ (
http://perl-xml.sourceforge.net/faq/ once again ;--) for more information.
 
M

Michel Rodriguez

Mark said:
Does anyone have some sample code I could examine to learn how to use the
XML::parser package when Style is set to Objects? E.g.,
[...]
I'm still pretty green at Perl, and maybe "now what?" is obvious to more
experienced Perl coders, but not me...
[...]

Is there any specific reason why you chose to use XML::parser?
XML::parser is really a low level module, that IMHO should not be used
directly.

I think XML::Simple would work just fine in your case. It will read the
XML into a Perl data structure. Or try XML::LibXML, or XML::Twig if you
need more features. Look at the FAQ (
http://perl-xml.sourceforge.net/faq/ once again ;--) for more information.
 
T

thumb_42

In comp.lang.perl.misc Michel Rodriguez said:
Is there any specific reason why you chose to use XML::parser?
XML::parser is really a low level module, that IMHO should not be used
directly.

Of the XML solutions out there that I've seen, (mostly java ones) I still
find XML::parser (in event mode) the easiest/fastest. (I haven't messed with
that many perl modules for XML though)

Takes a little bit to get used to at first, since the documentation
introduces it as some sort of tree that is rather difficult to grok, but if
you skip passed that and get to the event model, it's easy to process XML.
(seems pretty fast too). Only difficult thing of XML::parser I've ever been
mixed up with was dealing with name spaces.

I've done some pretty wild stuff with XML::parser, and can say that it's
enjoyable. :)

I'd almost rather build my own custom tree than to deal with any DOM level
stuff. (unless I were writing XML)

Jamie
 
B

Ben Morrow

Michel Rodriguez said:
Is there any specific reason why you chose to use XML::parser?
XML::parser is really a low level module, that IMHO should not be used
directly.

I was under the impression that XML::parser was more-or-less
deprecated now, in favour of SAX... is that not correct?

Ben
 
B

Bart Lateur

Ben said:
I was under the impression that XML::parser was more-or-less
deprecated now, in favour of SAX... is that not correct?

That was what Matt Sergeant, maintainer of most of the XML modules,
seems to be wanting, yes. I'm not sure I agree.
 
M

Michel Rodriguez

Bart said:
That was what Matt Sergeant, maintainer of most of the XML modules,
seems to be wanting, yes. I'm not sure I agree.

I think XML::parser should be deprecated for "regular users". It is not
actively supported any more, and if you want to learn a streaming
interface, you might as well learn SAX (although SAX is lower-level than
XML::parser if you can believe it).

XML::parser is still useful as a basic XML parsing layer for other
modules though, that give a higher-level API: XML::Simple (can also work
on top of a SAX parser), XML::Twig, XML::DOM (although I would not
recommend using XML::DOM, but rather XML::LibXML), XML::pYX...

In the last 2/3 years the perl XML community has largely shifted from
XML::parser (and expat), to XML::LibXML (based on libxml2), which is a
lot more powerful, giving you XML parsing but also DOM, XPath,
XInclude... The only problems with XML::LibXML is that as the library
and the module are both still work in progress, they have sometimes
stability issues, you have to find the proper combination of
libxml2/XML::LibXML that works and stick to it for a while
(<pet_peeve>and that said, XML::parser has had its problems too: the
interface to the DTD events for example has changed quite a few times in
incompatible ways</pet_peeve> ;--)

A plus of XML::parser is also that it comes installed with Activestate
Perl, which might make it easier for people who don't have a compiler on
their system (but note that XML::SAX::purePerl does not need to be
compiled and can work in such an environment too).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top