XML XMLparser n SAX DOM n n n Slides: 13 Download presentation XML文書の処理 XMLパーサ(parser)の概念 n パーサの規格(SAX, DOM、他) n n n パーサを「取り出す」ための仕組み SAXParser, SAXParser. Factory Document. Builder, Document. Builder. Factory パーサの取得のパターン Document. Builder. Factory dbf = Document. Builder. Factory. new. Instance(); Document. Builder db = dbf. new. Document. Builder(); DOMツリーの表現 n n org. w 3 c. dom. Node インタフェース 主要な Node のサブインタフェース Document Element Attr Text 再帰的な呼び出しの例 public void search( Node node ) { if( node. get. Node. Name(). equals(“A”) ) print( node ); Node. List list = node. get. Child. Nodes(); if( list. get. Length() == 0 ) return; for( int i=0; i<list. get. Length(); i++ ) { search( list. item(i)); } 属性の処理の例 Named. Node. Map nnp = node. get. Attributes(); for( int i=0; i<nnp. get. Length(); i++ ) { Node n = nnp. item( i ); if( n. get. Node. Type() == Node. ATTRIBUTE_NODE ) { Attr a = (Attr)n; String name = a. get. Name(); String value = a. get. Value(); ツリーの視覚化とSwing n n javax. swing. JTree クラス javax. swing. tree パッケージ Default. Mutable. Tree. Node クラス n javax. swing. event パッケージ DOMツリーの視覚化(1) dbf =Document. Builder. Factory. new. Instance(); db = dbf. new. Document. Builder(); Document doc = db. parse( source ); Node root. Tag = doc. get. First. Child(); root = new. Default. Mutable. Tree. Node( root. Tag. get. Node. Name() ); tree = new JTree( root ); add. Children( root. Tag, root ); DOMツリーの視覚化(2) public void add. Children( Node node, Default. Mutable. Tree. Node branch ) { : Default. Mutable. Tree. Node tn = new Default. Mutable. Tree. Node( child. get. Node. Name() ); branch. add( tn ); add. Children( child, tn );