gpt4 book ai didi

java - 如何扩展XMLStreamReader?

转载 作者:行者123 更新时间:2023-12-01 18:30:55 26 4
gpt4 key购买 nike

http://docs.oracle.com/javaee/5/api/javax/xml/stream/XMLStreamReader.html

我想扩展 XMLStreamReader,但它是一个接口(interface)。

我想做这样的事情:

public class GraphDataStreamReader extends XMLStreamReader{

public GraphDataStreamReader(){
super();
}

public void nextStartElement() throws XMLStreamException {
do {
super.next();
} while (!super.isStartElement());
}
}

其他类:

XMLInputFactory factory = XMLInputFactory.newInstance();
GraphDataStreamReader streamReader = (GraphDataStreamReader) factory.createXMLStreamReader(new FileReader(xmlFile)); //returns XMLStreamReader
streamReader.next(); //method from XMLStreamReader
streamReader.nextStartElement(); //method from GraphDataStreamReader

这可能吗?如果是,那么如何?

最佳答案

以下是如何扩展 StreamReaderDelegate :

class GraphDataStreamReader extends StreamReaderDelegate {
public GraphDataStreamReader(XMLStreamReader reader) {
super(reader);
}

public void nextStartElement() throws XMLStreamException {
do {
super.next();
} while (!super.isStartElement());
}
}

XMLInputFactory factory = XMLInputFactory.newInstance();
GraphDataStreamReader streamReader = new GraphDataStreamReader(
factory.createXMLStreamReader(fileInputStream));
streamReader.next();
streamReader.nextStartElement();

关于java - 如何扩展XMLStreamReader?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24265873/

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