hotshot has asked for the wisdom of the Perl Monks concerning the following question:

hello !

Supposed I have an XML document and I parsed it using the XML::Parser module. Does the parse/parsefile functions return a structure (array/hash/ref) that contains the XML doc so I could look for something in it? or do I have to parse everytime and use the start/end/char handler for that, couldn't understand completely from the documantation

Thanks.

Hotshot

Edit kudra, 2002-05-28 Changed title

  • Comment on searching in document parsed by XML::Parser

Replies are listed 'Best First'.
Re: XML::Parser
by Joost (Canon) on May 28, 2002 at 14:11 UTC
    from the manual:

    parse(SOURCE [, OPT => OPT_VALUE [...]])

    The SOURCE parameter should either be a string containing the whole XML document, or it should be an open IO::Han­dle. Constructor options to XML::Parser::Expat given as keyword-value pairs may follow the SOURCE parameter. These override, for this call, any options or attributes passed through from the XML::Parser instance.

    A die call is thrown if a parse error occurs. Otherwise it will return 1 or whatever is returned from the Final han­dler, if one is installed. In other words, what parse may return depends on the style.

    So, if you want a structure, use the 'Tree' STYLE, if you want handlers, use the 'Subs' STYLE.

    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: XML::Parser
by mirod (Canon) on May 28, 2002 at 17:11 UTC

    If you want to access the document content you might be better off using one of the modules built on top of XML::Parser:

    • XML::Simple will work nicely if your XML represents data (it does not process mixed content properly),
    • XML::DOM is a little dangerous to use IMHO, but if you know the DOM it can be an option,
    • XML::XPath is very nice, it allows you to use XPath expressions to retrieve information from the document,
    • XML::Twig of course is always a wise choice (I am a little biaised there ;--).

    You can also use a module not based on XML::Parser. XML::libXML for example builds a DOM, and allows you to use XPath to query the document.