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

我想扩展XMLStreamReader,但这是一个接口。

我想做类似的事情:

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();

10-05 19:36