In XML: TWIG, how to stop parsing as soon as you find an element of interest

I just want to analyze the xml element of interest (for example, see below: a class element with a name is equal to mathematics), and I want to stop as soon as the first element that hits this condition is parsed. (since there is only one class whose name is mathematics, there is no need to continue as soon as the element is already found).

However, if I implement it as follows, the code continues to read the entire file after it finds the element that interests me (the xml file is very long, so it takes a lot of time). my question is how to stop it when parsing the first element of a class named = math?

my $twig = new XML::Twig(TwigRoots => {"class[\@name='math']" => \&class}); $twig->parsefile( shift @ARGV );

In addition, I also want to remove this class from the xml file (not only from memory) after parsing it, so the next time when parsing the class with other names, the class element will not be parsed. Can this be done?

+4
source share
1 answer

It seems that you are looking for XML :: Twig finish_print and finish_now :

finish_print

Stop processing twigs, flushing branches and continuing to print a document as quickly as possible. Use this method when changing a document and modification is done.

finish_now

Stops branch processing, does not complete the analysis of the document (which really would not have been well formed after the point where finish_now is called). Execution resumes after Lparse> or parsefile. The branch content is what has been parsed so far (all open items at that time are considered final_now closed).

+6
source

All Articles