in reply to XML::Parser Style=>Subs and undefined subroutines

I am new to XML::Parser.

May I suggest that you stay this way? ;--)

Seriously, XML::Parser is well on its way to deprecation, so you should really try other options: XML::LibXML, XML::Twig or a SAX-based module. XMl;;LibXML will be faster, XML::Twig more convenient, and SAX is a pain but at least it is a standard.

  • Comment on Re: XML::Parser Style=>Subs and undefined subroutines

Replies are listed 'Best First'.
Re: XML -- Kudos to XML::Twig
by rkg (Hermit) on Oct 01, 2003 at 15:34 UTC
    Wow.

    I am blown away by the power and ease-of-learning of XML::Twig.

    I jumped in without studying the docs carefully... and it seemed whenever I needed something ("Wouldn't it be great to call 'parent' here to get the parent?", and "Wouldn't it be great if 'att' generalized to 'atts', to give me all of them?" and "Wouldn't it be nice if 'first_child' generalized to 'children', to get all the kids?") and lo and behold they were all right there, with the API I'd expect.

    High praise for XML::Twig, and thanks, Mirod.

    (Likely this is a RTFM question, but how hard is it to get Twig to validate as well as parse...?)

    rkg

      First, thanks for the comments :--) You can also have a look at the tutorial, which seems to be the easiest way to grok the module for most people, as the POD is really more of a reference, at 2500 lines.

      how hard is it to get Twig to validate as well as parse?

      Rather hard actually. I could add (yet another!) option to use XML::Checker instead of XML::Parser, but I am not sure I trust it completely. So the easiest way is probably to install libxml2 and to use xmllint --valid on the output file.

Re: Re: XML::Parser Style=>Subs and undefined subroutines
by chanio (Priest) on Oct 02, 2003 at 03:26 UTC
    sorry, but what's wrong in using it like this...

    $xs = new XML::Parser; die ("Can't find file: $completePathFile $!") unless -f($completePat +hFile); $xs->setHandlers( Start => \&openElem, End => \&closElem, Char => \&heresTheData, Default => \&forgetit ); $xs->parsefile($completePathFile); ##now, parses and logic sub openElem{ } sub closElem{ } sub heresTheData{ } sub forgetit{ }
    You have to activate a flipflop variable when you open the desired tag. And deactivate it when this tag closes.

    In between, you are able to process the data that is received. Or waiting for other(s) flipflop(s) before processing the incoming data. You could leave it like this, and the file is going to parse perfectly ('though without any profit :).

    For me, this module is a great masterpiece. But I only know this one, because it is the one more easyly found at any WWW site.

      Nothing wrong here, there are just easier ways to code most XML processing than this.

      BTW you don't have to use a flipflop variable, you can just call current_element on the parser object to get the element in which you are (and context will give you the stack of open elements), man XML::Expat::Parser is an oft-overlooked resource that will give you the list of methods you can use on the parser object.

      But really, try using XML::LibXML or XML::Twig, have a look at the various comparisons at xmltwig.com (look at the bottom 3 articles), and you will see what a difference XPath can make.