/** * Created by IntelliJ IDEA. * User: Yasumasa Ashida * Date: 2005/12/03 * Time: 17:55:56 */ import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLEventReader; import javax.xml.stream.events.XMLEvent; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.EndElement; import javax.xml.namespace.QName; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; public class Tester { private static final String fileName = "test.xml"; private static final String ELEMENT = "element"; private static final String ELEMENT_ID = "id"; private static final String ATTR = "attribute"; private static final String ATTR_NAME = "name"; private static final String ATTR_VALUE = "value"; private static final String COMPONENT = "component"; public static void main(String[] args){ // 入力ファクトリを取得する XMLInputFactory xmlif = XMLInputFactory.newInstance(); try{ BufferedInputStream stream = new BufferedInputStream(new FileInputStream(fileName)); XMLEventReader reader = xmlif.createXMLEventReader(stream); while(reader.hasNext()){ XMLEvent e = reader.nextEvent(); analyzeElement(e,reader); analyzeComponent(e,reader); } reader.close(); }catch(XMLStreamException e){ e.printStackTrace(); }catch(FileNotFoundException e){ e.printStackTrace(); } } private static void analyzeElement(XMLEvent e,XMLEventReader reader) throws XMLStreamException{ if(e.isStartElement()){ StartElement se = (StartElement)e; if(se.getName().getLocalPart().equals(ELEMENT)){ Attribute attr = se.getAttributeByName(new QName(ELEMENT_ID)); System.out.println(attr.getValue()); analyzeAttributes(e,reader); } } } private static void analyzeComponent(XMLEvent e,XMLEventReader reader) throws XMLStreamException{ if(e.isStartElement()){ StartElement se = (StartElement)e; if(se.getName().getLocalPart().equals(COMPONENT)){ System.out.println(se.getName().getLocalPart()); analyzeElements(e,reader); } } } private static void analyzeAttributes(XMLEvent e,XMLEventReader reader) throws XMLStreamException{ boolean next_flg = false; while(reader.hasNext() && !next_flg ){ e = reader.nextEvent(); if(e.isStartElement()){ StartElement se = (StartElement)e; if(se.getName().getLocalPart().equals(ATTR)){ Attribute attr = se.getAttributeByName(new QName(ATTR_NAME)); System.out.print(attr.getValue() + ":"); attr = se.getAttributeByName(new QName(ATTR_VALUE)); System.out.println(attr.getValue()); }else{ next_flg = true; } }else if(e.isEndElement()){ EndElement ee = (EndElement)e; if(!ee.getName().getLocalPart().equals(ATTR)){ next_flg = true; } } } } private static void analyzeElements(XMLEvent e,XMLEventReader reader) throws XMLStreamException{ boolean next_flg = false; while(reader.hasNext() && !next_flg ){ e = reader.nextEvent(); if(e.isStartElement()){ analyzeElement(e,reader); }else if(e.isEndElement()){ EndElement ee = (EndElement)e; if(!ee.getName().getLocalPart().equals(ELEMENT) && !ee.getName().getLocalPart().equals(ATTR)){ next_flg = true; } } } } }#test.xml
<?xml version="1.0" encoding="Windows-31J" ?>
<test>
<element id="pepe" >
<attribute name="test" value="pc"/>
<attribute name="test" value="pc"/>
</element>
<elementid="popo" >
<attribute name="test" value="pc"/>
</element>
<component>
<elementid="pepe" >
<attribute name="test"value="pc"/>
<attribute name="test" value="pc"/>
</element>
<element id="popo" >
<attribute name="test" value="pc"/>
</element>
</component>
</test>
メモメモφ。/* from ネバーバード日記(on off) and MYCOM(on off) */
-->WEBサービストランザクション(on off)
メモメモφ。
メモメモφ。/* from オレンジニュース */
Ruby製Tracもどき。開発中(現バージョンは0.5)。利用例はこちら(on off)。
/* from momonga-dev.ja ML:03255 */
メモメモφ。/* from オレンジニュース */
メモメモφ。/* from オレンジニュース */