如何停止解析器?

发布于 2024-12-27 13:08:54 字数 305 浏览 1 评论 0原文

我正在使用 node.js 和 SAX 模块来解析我的 XML 文档。

当我找到我正在寻找的节点时,我想阻止(中止)解析器。

我尝试了 parser.close() 但收到此错误:

  if (this.error) throw this.error
                            ^
Error: Unexpected end
Line: 1
Column: 59

如何阻止它而不解析文档的其余部分,因为它对我没有用处?

谢谢

I'm using node.js and SAX module to parse my XML documents.

I would like to block(abort) the parser when I find the node I was looking for.

I tried parser.close() but I get this error:

  if (this.error) throw this.error
                            ^
Error: Unexpected end
Line: 1
Column: 59

How can I block it without parsing the rest of the document that it is not useful to me?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

最佳男配角 2025-01-03 13:08:54

如果您查看 SAX 模块的代码,您就可以明白抛出该错误的原因。

https://github.com/isaacs/sax-js /blob/master/lib/sax.js#L585

如果在设置错误后尝试调用 parser.write(),则会抛出错误。如果在解析过程中调用 parser.close() ,则可能会出现错误。

https://github.com/isaacs/sax-js /blob/master/lib/sax.js#L354

在执行完 parser.close() 之后不要调用 parser.write() ,你应该被设置。

If you look at the code for the SAX module, you can see why that error is thrown.

https://github.com/isaacs/sax-js/blob/master/lib/sax.js#L585

If you attempt to call parser.write() after an error is set, then it will throw an error. If you call parser.close() in the middle parsing, then it may set an error.

https://github.com/isaacs/sax-js/blob/master/lib/sax.js#L354

Don't don't call parser.write() after doing parser.close() and you should be set.

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