如何创建一个可以在发生“致命”错误时继续运行的 XMLStreamReader?错误?

发布于 2024-12-01 11:38:04 字数 992 浏览 0 评论 0原文

我需要寻找一个巨大的 XML 文件,从字节位置开始读取 XML 数据,基本上是读取一个块。我正在创建一个像这样的 XMLStreamReader

final XMLInputFactory factory = XMLInputFactory.newInstance();
reader = factory.createXMLStreamReader(inputStream);

该读取器可以从流中的任何位置开始读取,但我希望它跳过一些内容,直到找到标签的开头。我不想让它验证任何东西。

例如,它可以在 firstAttr 中间开始读取此内容:

rstAttr='12345' secondAttr='QWERTY'>
</endElement>
<startReadingAtLeastFromHere>
...

我真的不需要读取 ,但我希望解析器至少阅读 以及之后的所有内容。

我的问题是 factory.setXMLReporter(reporter) 没有被调用,所以我不能用它来告诉读者继续。

我暂时没有指定 javax.xml.stream.XMLInputFactory 系统属性,它默认为 Sun 的 XMLInputFactoryImpl。它使用 StaxErrorReporter,其中有一个名为 fContinueAfterFatalError 的字段,但我找不到如何更改其值,或如何替换 StaxErrorReporter和我自己的。

如果这太棘手,是否有任何基于拉取的 XML 解析器可以配置为忽略错误并继续阅读?

I need to seek into a huge XML file, start reading XML data from a byte position, basically reading a chunk. I'm creating an XMLStreamReader like this:

final XMLInputFactory factory = XMLInputFactory.newInstance();
reader = factory.createXMLStreamReader(inputStream);

This reader can start reading at any position in the stream, but I want it to skip stuff until it finds the start of a tag. I do not want it to validate anything.

For example, it could start reading this, in the middle of firstAttr:

rstAttr='12345' secondAttr='QWERTY'>
</endElement>
<startReadingAtLeastFromHere>
...

I don't really need to read the </endElement>, but I want the parser to read at least <startReadingAtLeastFromHere> and all content after that.

My problem is that factory.setXMLReporter(reporter) is not invoked, so I cannot use that to tell the reader to continue.

I'm not specifying the javax.xml.stream.XMLInputFactory system property for now, and it defaults to Sun's XMLInputFactoryImpl. It uses StaxErrorReporter, which has a field named fContinueAfterFatalError, but I can't find how to change its value, or how to replace the StaxErrorReporter with my own.

If that's too tricky, is there any pull-based XML parser that can be configured to ignore errors and keep reading?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文