XML n n XMLparser SAX DOM SAX Simple

  • Slides: 21
Download presentation

XML文書の処理 n n XMLパーサ(parser)の概念 パーサの規格(SAX, DOM、他) SAX (Simple API for XML ) DOM (Document

XML文書の処理 n n XMLパーサ(parser)の概念 パーサの規格(SAX, DOM、他) SAX (Simple API for XML ) DOM (Document Object Model)

パーサを取得するパターン(1) SAXParser. Factory spf = SAXParser. Factory. new. Instance(); SAXParser parser = spf. new.

パーサを取得するパターン(1) SAXParser. Factory spf = SAXParser. Factory. new. Instance(); SAXParser parser = spf. new. SAXParser(); XMLReader reader = parser. get. XMLReader();

DOMによる処理 DOM(Document Object Model ) n 文書の構造を記述(DOMツリー) n

DOMによる処理 DOM(Document Object Model ) n 文書の構造を記述(DOMツリー) n

パーサの取得のパターン Document. Builder. Factory bf = Document. Builder. Factory. new. Instance(); Document. Builder db

パーサの取得のパターン Document. Builder. Factory bf = Document. Builder. Factory. new. Instance(); Document. Builder db = dbf. new. Document. Builder();

DOMツリーの表現 n n org. w 3 c. dom. Node インタフェース 主要な Node のサブインタフェース Document

DOMツリーの表現 n n org. w 3 c. dom. Node インタフェース 主要な Node のサブインタフェース Document Element Attr Element Text

XML文書の構造の例 <document> <title>Java and XML</title> Let's begin, now! <image source="java. png" width="512" height="400"/> </document>

XML文書の構造の例 <document> <title>Java and XML</title> Let's begin, now! <image source="java. png" width="512" height="400"/> </document>

DOMツリーによる表現 Document Element<document> Element<title> Text “Java and…” Text ”Let’s Begin…” Attributes Element<image>

DOMツリーによる表現 Document Element<document> Element<title> Text “Java and…” Text ”Let’s Begin…” Attributes Element<image>

再帰的な呼び出しの例 public void print. Node( Node node ) { Node. List list = node.

再帰的な呼び出しの例 public void print. Node( Node node ) { Node. List list = node. get. Child. Nodes(); if( list. get. Length() == 0 ) return; for( int i=0; i<list. get. Length(); i++ ) { print. Node( list. item(i)); } }

属性を扱う例(1) if( node. get. Node. Type() == Node. ELEMENT_NODE ) { Named. Node. Map

属性を扱う例(1) if( node. get. Node. Type() == Node. ELEMENT_NODE ) { Named. Node. Map nnp = node. get. Attributes(); }

属性を扱う例(2) for( int i=0; i<nnp. get. Length(); i++ ) { Node n = nnp.

属性を扱う例(2) 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; System. out. print( "[" + a. get. Name() + "=" + a. get. Value() + "]" ); } }