在 XML:TWIG 中,一旦找到感兴趣的元素如何停止解析

发布于 2024-09-12 05:38:37 字数 427 浏览 5 评论 0原文

我只想解析 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2024-09-19 05:38:37

看来您正在寻找的是 XML::Twig 的 finish_printfinish_now

完成打印

停止树枝处理,冲洗树枝并继续完成打印
尽快处理该文档。使用
修改文档时使用此方法
修改完成。

立即完成

停止树枝处理,未完成文档解析(其中
实际上可能格式不正确
在 finish_now 所在点之后
称为)。执行后恢复
Lparse>或解析文件调用。内容
树枝的内容已经被解析了
远(当时所有开放的元素
finish_now 被调用被认为
已关闭)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文