in reply to accessing root tags when using XML::Twig and twig_roots

I think that what you are looking for is the start_tag_handlers option, which lets you call a handler as soon as the start tag of an element has been parsed.

In the handler you can die (provided the call to parse is wrapped in an eval) or do whatever you want, including setting regular handlers.

  • Comment on Re: accessing root tags when using XML::Twig and twig_roots

Replies are listed 'Best First'.
Re^2: accessing root tags when using XML::Twig and twig_roots
by John M. Dlugosz (Monsignor) on May 04, 2006 at 19:25 UTC
    If I use a start_tag_handler for the root element, then I can't have "roots" that are a subset of the whole document.

    If I set the "roots" within the first callback, what happens to the printout? I have a partial open going on and it hasn't started printing outside of roots yet. Whatever happens, will it be well-behaved?

      Couldn't you do two passes against the file? First create a twig with the start_tag_handler to get whatever it is you need. Then use that information to create a second twig for the actual processing. Would that work or am I missing something? As mirod mentions some sample code and data might help clear things up.
        Yes, that is an option, now that I know how to quit once I find what was needed. Someone said that die from a handler will do the trick, presumably without leaving the module in a funny state.

      What did you try? I find it easier to get some code to work than to answer questions like these ;--(