ASIPの徒然なるままに このページをアンテナに追加 RSSフィード

2007-12-18

XMLEventSubReader 09:11 XMLEventSubReaderを含むブックマーク XMLEventSubReaderのブックマークコメント

XMLStreamReader改のXMLEventReader版を作ってみた。

import javax.xml.stream.util.EventReaderDelegate; 
import javax.xml.stream.events.XMLEvent; 
import javax.xml.stream.XMLStreamException; 
import javax.xml.stream.XMLEventReader; 
import javax.xml.stream.XMLStreamConstants; 
 
public class XMLEventSubReader extends EventReaderDelegate { 
    protected String targetNode; 
    protected boolean breakTargetNode; 
    protected int countTargetNode; 
    protected XMLEvent currentEvent; 
 
    public XMLEventSubReader(XMLEventReader delegateReader,XMLEvent event) { 
        super(delegateReader); 
        this.currentEvent = event; 
        if(currentEvent.isStartElement()){ 
            this.targetNode = currentEvent.asStartElement().getName().getLocalPart(); 
        }else{ 
            this.targetNode = null; 
        } 
        this.breakTargetNode = false; 
    } 
 
    @Override 
    public boolean hasNext() { 
        if ( this.breakTargetNode) { 
            return false; 
        } else { 
            return this.getParent().hasNext(); 
        } 
    } 
 
    @Override 
    public XMLEvent nextEvent() throws XMLStreamException { 
 
        if(currentEvent.isStartElement()){ 
            if ( currentEvent.asStartElement().getName().getLocalPart().equals(this.targetNode)) { 
                countTargetNode++; 
                this.breakTargetNode = false; 
            } 
        }else if(currentEvent.isEndElement()){ 
 
            if ( currentEvent.asEndElement().getName().getLocalPart().equals(this.targetNode)) { 
                countTargetNode--; 
                this.breakTargetNode = (countTargetNode == 0); 
            } 
        }else if(currentEvent.isEndDocument()){ 
 
            this.breakTargetNode = true; 
 
        } 
        currentEvent = this.getParent().nextEvent(); 
 
        return currentEvent; 
    } 
 
    public XMLEvent currentEvent(){ 
        return currentEvent; 
    } 
 
} 

テストコードはこんな感じ。

import javax.xml.stream.XMLStreamReader; 
import javax.xml.stream.XMLInputFactory; 
import javax.xml.stream.XMLEventReader; 
import javax.xml.stream.events.XMLEvent; 
import java.io.BufferedInputStream; 
import java.io.FileInputStream; 
 
 
public class Tester { 
 
    private static final String fileName = "D:\\kuro\\nova\\nova-xml\\classes\\test.xml"; 
 
    public static void main(String[] args) throws Exception{ 
         
 
        // 入力ファクトリを取得する 
        XMLInputFactory xmlif = XMLInputFactory.newInstance(); 
 
        BufferedInputStream stream = new BufferedInputStream(new FileInputStream(fileName)); 
         
        XMLEventReader reader = xmlif.createXMLEventReader(stream); 
        while(reader.hasNext()){ 
            xe = reader.nextEvent(); 
            if(xe.isStartElement()){ 
                if("b".equals(xe.asStartElement().getName().getLocalPart())){ 
                    XMLEventSubReader subReader = new XMLEventSubReader(reader,xe); 
                    for(;subReader.hasNext();subReader.nextEvent())  { 
 
                        //カレントノードのサブツリーのみを処理 
                        if(subReader.currentEvent().isStartElement()){ 
                            System.out.println("sub:" + subReader.currentEvent().asStartElement().getName().getLocalPart()); 
                        }else if(subReader.currentEvent().isEndElement()){ 
                            System.out.println("sub:" + subReader.currentEvent().asEndElement().getName().getLocalPart()); 
                        } 
 
                    } 
                } 
 
            } 
        } 
 
 
    } 
} 
Connection: close