gpt4 book ai didi

java - StAX 事件过滤器 StackOverflowError

转载 作者:行者123 更新时间:2023-11-30 05:02:27 25 4
gpt4 key购买 nike

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

如果有人知道问题所在,我将不胜感激。

最佳答案

我无法重现您的问题,以下基于您的描述对我有用。我是否遗漏了你的问题中的某些内容?:

有趣的元素过滤器

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>

关于java - StAX 事件过滤器 StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6204585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com