在 XML:TWIG 中,一旦找到感兴趣的元素如何停止解析
我只想解析 xml 中感兴趣的元素(例如,参见下文:名称等于 math 的类元素),并且我想在解析满足此条件的第一个元素后停止。 (由于只有一个名为 math 的类,因此一旦找到元素就无需继续)。
但是,如果我按如下方式实现,代码在找到我感兴趣的元素后会继续读取整个文件(xml 文件很长,因此需要很长时间)。我的问题是,一旦解析了 name = math 的第一个类元素,如何停止它?
<代码> 我的 $twig = new XML::Twig(TwigRoots => {"class[\@name='math']" => \&class}); $twig->parsefile( shift @ARGV );
另外,我还想在解析该类后从 xml 文件(不仅仅是从内存中)删除该类,以便下次解析具有其他名称的类时,类元素将不会被解析。可以这样做吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您正在寻找的是 XML::Twig 的
finish_print
和finish_now
:It seems what you're looking for are XML::Twig's
finish_print
andfinish_now
: