简体   繁体   English

如何获取XMLEventAllocator的实例?

[英]how to get an instance of an XMLEventAllocator?

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 XMLEvent using an XMLEventAllocator when necessary. 我试图遵循使用sun的Java EE 5的Cursor-to-Event示例来解析使用StAX XML的建议方法。您应该通过Cursor API遍历XML并在必要时使用XMLEventAllocator分配XMLEvent


Awkwardly, sun's own example does not compile (at least not with JDK 1.6, even with 1.5 code compliance). 尴尬的是,sun自己的例子没有编译(至少没有JDK 1.6,即使符合1.5代码)。 The example tries to instantiate an allocator via new , but the according implementation classes in the JDK are not accessible externally. 该示例尝试通过new实例化分配器,但JDK中的相应实现类无法从外部访问。


After reading the JavaDocs and searching the web I have found literally nothing. 在阅读了JavaDocs并在网上搜索后,我发现什么都没有。


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. 可以从头开始实现XMLEventAllocator接口,但是当JDK中有非常好的实现时,它似乎真的是错误的,除了不是StAX专家之外,很难做到正确。

I would not use that example as a best practice for using StAX. 我不会将此示例用作使用StAX的最佳实践。 With StAX you have two approaches XMLStreamReader and XMLEventReader. 使用StAX,您有两种方法XMLStreamReader和XMLEventReader。 Both give you an API for accessing the events for a depth-first traversal of an XML document. 两者都为您提供了一个API,用于访问事件以进行深度优先遍历XML文档。 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. 使用XMLStream阅读器,您可以根据事件类型从XMLStreamReader请求信息,并使用XMLEventReader为您提供表示原始事件的对象。

I recommend using the XMLStreamReader API directly. 我建议直接使用XMLStreamReader API。

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. 除了支持Blaise建议直接使用游标API,即使你确实想要使用Event API,也绝对不需要定义自定义XMLEventAllocation实现。 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. 你可以这样做(比如添加一些与Even对象一起传递的数据),但这将是一种高级技术。

So if you want to use Event API, just ask XMLInputFactory to produce XMLEventReader, like so: 因此,如果您想使用Event API,只需要求XMLInputFactory生成XMLEventReader,如下所示:

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

or if you have an XMLStreamReader: 或者如果你有一个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. 男孩,我不知道为什么教程有那么愚蠢的小代码 - 它没有任何意义。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM