如何获取 XMLEventAllocator 的实例?

发布于 2024-10-08 22:09:33 字数 533 浏览 5 评论 0原文

我正在尝试遵循使用 sun 的 Java EE 5 的光标到事件示例。您应该通过 Cursor API 遍历 XML 并使用 XMLEventAllocator 分配 XMLEvent必要时。


令人尴尬的是,sun 自己的示例无法编译(至少在 JDK 1.6 中无法编译,甚至在 1.5 代码合规性下也是如此)。该示例尝试通过 new 实例化分配器,但 JDK 中的相应实现类无法从外部访问。


阅读 JavaDocs 并搜索网络后,我几乎什么也没找到。


一个人可以从头开始实现 XMLEventAllocator 接口,但是当 JDK 中有完美的实现时,这似乎是错误的,而且不是 StAX 方面的专家很难做到正确。

I am trying to follow the recommended way of parsing XML with StAX using sun's Cursor-to-Event Example for Java EE 5. You are supposed to traverse the XML via the Cursor API and allocate an XMLEventusing an XMLEventAllocator when necessary.

Awkwardly, sun's own example does not compile (at least not with JDK 1.6, even with 1.5 code compliance). The example tries to instantiate an allocator via new, but the according implementation classes in the JDK are not accessible externally.

After reading the JavaDocs and searching the web I have found literally nothing.

One could implement the XMLEventAllocator interface from scratch, but it seems really wrong, when there are perfectly good implementations in the JDK, besides not being an expert in StAX makes it difficult to get it right.

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

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

发布评论

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

评论(2

jJeQQOZ5 2024-10-15 22:09:33

我不会使用该示例作为使用 StAX 的最佳实践。使用 StAX,您有两种方法 XMLStreamReader 和 XMLEventReader。两者都为您提供了一个 API,用于访问事件以深度优先遍历 XML 文档。使用 XMLStream reader,您可以根据事件类型从 XMLStreamReader 请求信息,并且使用 XMLEventReader,您将获得表示原始事件的对象。

我建议直接使用 XMLStreamReader API。

I would not use that example as a best practice for using StAX. With StAX you have two approaches XMLStreamReader and XMLEventReader. Both give you an API for accessing the events for a depth-first traversal of an XML document. With XMLStream reader you can request info from the XMLStreamReader based on the event type, and with XMLEventReader you are given objects representing the original event.

I recommend using the XMLStreamReader API directly.

你没皮卡萌 2024-10-15 22:09:33

除了赞同 Blaise 直接使用游标 API 的建议之外,即使您确实想使用事件 API,也绝对不需要定义自定义 XMLEventAllocation 实现。如果您愿意,您可以这样做(例如添加一些要与 Even 对象一起传递的数据),但这将是一种高级技术。

因此,如果您想使用事件 API,只需要求 XMLInputFactory 生成 XMLEventReader,如下所示:

XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(new FileInputStream("file.xml"));

或者如果您有 XMLStreamReader:

XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(streamReader);

这就是您需要做的全部事情。

天哪,我不知道为什么教程中有那么愚蠢的小代码——它毫无意义。 :-)

Beyond seconding Blaise's suggestion of just using cursor API directly, even if you do want to use Event API there is absolutely no need to define custom XMLEventAllocation implementation. You can do that if you want to (like add some data to be passed along with Even objects), but it would be an advanced technique.

So if you want to use Event API, just ask XMLInputFactory to produce XMLEventReader, like so:

XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(new FileInputStream("file.xml"));

or if you have an XMLStreamReader:

XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(streamReader);

and that's all you need to do.

Boy, I have no idea why tutorial has that silly little piece of code -- it makes no sense whatsoever. :-)

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