in XML:TWIG, how to stop parsing once find the interested element

拈花ヽ惹草 提交于 2019-12-01 17:31:24

问题


I only want to parse an interested element of xml (e.g. see below: class element with name equals to math) and I want to stop once the first element hitting this condition is parsed. (since There is only one class whose name is math, it is unnecessary to continue once the element is already found).

However, if I implement as follows, the code continues to read the whole file after it found the element i am interested (the xml file is very long so it takes long time). my question is how to stop it once the first class element with name = math is parsed?

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

besides, I also want to delete this class from xml file (not only from memory) after it is parsed so that next time when parsing a class with other names, the class element will not be parsed. Is it possible to do that?


回答1:


It seems what you're looking for are XML::Twig's finish_print and finish_now :

finish_print

Stops twig processing, flush the twig and proceed to finish printing the document as fast as possible. Use this method when modifying a document and the modification is done.

finish_now

Stops twig processing, does not finish parsing the document (which could actually be not well-formed after the point where finish_now is called). Execution resumes after the Lparse> or parsefile call. The content of the twig is what has been parsed so far (all open elements at the time finish_now is called are considered closed).



来源:https://stackoverflow.com/questions/3384674/in-xmltwig-how-to-stop-parsing-once-find-the-interested-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!