如何停止解析器?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看 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.