java - StAX EventFilter产品

  显示原文与译文双语对照的内容

我有一个EventFilter实现,它应该过滤 <InterestingElement> 启动元素事件:


class InterestingElementFilter implements EventFilter {
 @Override
 public boolean accept(XMLEvent event)
 {
 if (event.isStartElement()) {
 StartElement startElement = event.asStartElement();
 String name = startElement.getName().getLocalPart();
 if (name.equals("InterestingElement")) {
 return true;
 }
 }
 return false;
 }
}

然后我创建一个过滤的阅读器:


XMLEventReader interestingElementReader = xmlInputFactory.createFilteredReader(xmlEventReader, new InterestingElementFilter());

呼叫时


startElementReader.peek();

我知道了


java.lang.StackOverflowError
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipSpaces(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.seekCloseOfStartTag(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
at com.sun.xml.internal.stream.XMLEventReaderImpl.peek(Unknown Source)
at javax.xml.stream.util.EventReaderDelegate.peek(Unknown Source)
at com.sun.xml.internal.stream.EventFilterSupport.peek(Unknown Source)
at com.sun.xml.internal.stream.EventFilterSupport.peek(Unknown Source)
at com.sun.xml.internal.stream.EventFilterSupport.peek(Unknown Source)
at com.sun.xml.internal.stream.EventFilterSupport.peek(Unknown Source)

如果有人知道问题是什么,我会感激不尽。

时间: 作者:

我无法重现你的问题,基于你的描述为我工作。 我从你的问题中漏掉了什么?

InterestingElementFilter


import javax.xml.stream.EventFilter;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

class InterestingElementFilter implements EventFilter {

 public boolean accept(XMLEvent event)
 {
 if (event.isStartElement()) {
 StartElement startElement = event.asStartElement();
 String name = startElement.getName().getLocalPart();
 if (name.equals("InterestingElement")) {
 return true;
 }
 }
 return false;
 }

}

演示


import java.io.FileInputStream;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.XMLEvent;

public class Demo {

 public static void main(String[] args) throws Exception {
 XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
 XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream("input.xml"));
 InterestingElementFilter filter = new InterestingElementFilter();
 XMLEventReader interestingElementReader = xmlInputFactory.createFilteredReader(xmlEventReader, filter);

 while(interestingElementReader.hasNext()) {
 XMLEvent xmlEvent = interestingElementReader.peek();
 if(xmlEvent.isStartElement()) {
 System.out.println(xmlEvent.asStartElement().getName());
 }
 interestingElementReader.next();
 }
 }

}

Input.xml


<?xml version="1.0" encoding="UTF-8"?>
<root>
 <InterestingElement>
 <child>Hello World</child>
 </InterestingElement>
</root>

作者:
...