StAX EventFilter StackOverflowError

时间:2011-06-01 16:31:51

标签: java xml stax

我有这个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)

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

1 个答案:

答案 0 :(得分:2)

我无法重现您的问题,以下根据您的说明适合我。我错过了你的问题吗?:

<强> 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>