accessing root tags when using XML::Twig and twig_roots
John M. Dlugosz
created: 2006-05-04 11:23:38
I'm using XML::Twig based on the "Building an XML Filter" example from the docs. It is a good fit, as I only need to modify or remove a small number of nodes, and everything else gets passed through.

However, I need to access some attributes from the root element. If I don't use twig_roots then it has to load the whole file and it's not as trivial to simply not print nodes to be removed. It kind of defeats the main feature of Twig.

Is there a good way to read the attributes from the top-level element, and then configure the twigs (and print outside roots) and callbacks based on what I found? How to get what was already read to print seemlessly with what it will start handling differently?

Another idea is to start processing, then quit after the first start tag. Is there a flag or something that can be set from the callback to "cancel" and return from parse() without reading any more from the file?

—John

2006-05-05 Retitled by GrandFather, as per Monastery guidelines
Original title: 'accessing root tags when XML::Twig and twig_roots'

Re: accessing root tags when using XML::Twig and twig_roots
created: 2006-05-04 12:53:45

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.

Re^2: accessing root tags when using XML::Twig and twig_roots
created: 2006-05-04 15:25:00
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?

Re^3: accessing root tags when using XML::Twig and twig_roots
created: 2006-05-04 15:37:56

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

Re^3: accessing root tags when using XML::Twig and twig_roots
created: 2006-05-04 16:27:05
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.
Re^4: accessing root tags when using XML::Twig and twig_roots
created: 2006-05-05 17:14:25
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.

perlmonks.org content © perlmonks.org and Argel, John M. Dlugosz, mirod

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03