简体   繁体   English

XMLStreamReader 和 XMLEventReader 有什么区别?

[英]What is the difference between XMLStreamReader and XMLEventReader?

I surf through the web.我在网上冲浪。 I found that the XMLStreamReader is Cursor style API for parsing XML .我发现XMLStreamReader用于解析 XML 的 Cursor 样式 API And XMLEventReader is Iterator style API for Parsing XML .Could any one tell me in detail? XMLEventReader用于解析 XML 的 Iterator 风格的 API。有人能详细告诉我吗?

Have a look at explanation: https://www.ibm.com/developerworks/library/x-stax1/看看解释: https : //www.ibm.com/developerworks/library/x-stax1/

Both XMLStreamReader and XMLEventReader allow the application to iterate over the underlying XML stream on its own. XMLStreamReader 和 XMLEventReader 都允许应用程序自行迭代底层 XML 流。 The difference between the two approaches lies in how they expose pieces of the parsed XML InfoSet.这两种方法之间的区别在于它们如何公开已解析的 XML InfoSet 的各个部分。 The XMLStreamReader acts as a cursor that points just beyond the most recently parsed XML token and provides methods for obtaining more information about it. XMLStreamReader 充当光标,指向最近解析的 XML 标记之外,并提供获取有关它的更多信息的方法。 This approach is very memory-efficient as it does not create any new objects.这种方法非常节省内存,因为它不会创建任何新对象。 However, business application developers might find XMLEventReader slightly more intuitive because it is actually a standard Java Iterator that turns the XML into a stream of event objects.但是,业务应用程序开发人员可能会发现 XMLEventReader 稍微更直观,因为它实际上是一个标准的 Java 迭代器,可将 XML 转换为事件对象流。 Each event object in turn encapsulates information pertaining to the particular XML structure it represents.每个事件对象依次封装与其所表示的特定 XML 结构有关的信息。 Part 2 of this series will provide a detailed description of the event iterator-based API.本系列的第 2 部分将详细描述基于事件迭代器的 API。 As to which API style to use depends on the situation.至于使用哪种 API 样式要视情况而定。 The event iterator-based API represents a more object-oriented approach than the cursor-based API.基于事件迭代器的 API 代表了一种比基于游标的 API 更面向对象的方法。 As such, it is easier to apply in modular architectures, because the current parser state is reflected in the event object;因此,它更容易应用于模块化架构,因为当前的解析器状态反映在事件对象中; thus, an application component does not need access to the parser/reader while processing the event.因此,应用程序组件在处理事件时不需要访问解析器/读取器。 Furthermore, it is possible to create an XMLEventReader from an XMLStreamReader using XMLInputFactory's createXMLEventReader(XMLStreamReader) method.此外,可以使用 XMLInputFactory 的 createXMLEventReader(XMLStreamReader) 方法从 XMLStreamReader 创建 XMLEventReader。

I think the difference is that the stream reader actually represents the events.我认为不同之处在于流阅读器实际上代表了事件。

XMLEvent event = eventReader.nextEvent();    
if(event.getEventType() == XMLStreamConstants.START_ELEMENT){
    StartElement startElement = event.asStartElement();
    System.out.println(startElement.getName().getLocalPart());
}

vs对比

streamReader.next();
if(streamReader.getEventType() == XMLStreamReader.START_ELEMENT){
    System.out.println(streamReader.getLocalName());
}

So an extra event object created each time for the event reader.所以每次为事件读取器创建一个额外的事件对象。 The overhead might be significant as there are many, many events.由于有很多很多事件,开销可能很大。

两者之间的一个区别是XMLEventReader支持peek() ,而XMLStreamReader不支持。

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

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