Presentation is loading. Please wait.

Presentation is loading. Please wait.

第8回 2007年6月15日 応用Java (Java/XML).

Similar presentations


Presentation on theme: "第8回 2007年6月15日 応用Java (Java/XML)."— Presentation transcript:

1 第8回 2007年6月15日 応用Java (Java/XML)

2 前回までやったこと 「XMLパーサ」-- Java でXMLを処理 javax.xml.stream パッケージのパーサ
SAX(Simple API for XML) のパーサ DOM(Document Object Model)のパーサ 応用(1) – テーブルの利用 「要素の出現回数を調べる」 応用(2) – フィルタ的な処理 応用(3) – DOMツリーの検索 応用(4) – DOMツリーの視覚化

3 本日(6/15)の講義内容 検索機能の強化と効率化 XPath の利用 XPath のパッケージとAPI javax.xml.xpath

4 復習)パーサの取得のパターン DocumentBuilderFactory bf =
DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder();

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

6 DOMツリーによる表現

7 検索処理の例(要素名の検索) public void search( Node node ) {
if( node.getNodeName().equals(“A”) ) print( node ); NodeList list = node.getChildNodes(); if( list.getLength() == 0 ) return; for( int i=0; i<list.getLength(); i++ ) { search( list.item(i)); }

8 少し複雑な場合(属性の検索 1) public void search( Node node, String name ) {
if( node.getNodeType() == Node.ELEMENT_NODE ) { Attr attr = searchAttr( node, name ); if( attr != null ) printFullName( node, attr ); } NodeList list = node.getChildNodes(); if( list.getLength() == 0 ) return; for( int i=0; i<list.getLength(); i++ ) { search( list.item(i), name ); } }

9 少し複雑な場合(属性の検索 2) public Attr searchAttr( Node node, String name ) {
NamedNodeMap nnp = node.getAttributes(); for( int i=0; i<nnp.getLength(); i++ ) { Node n = nnp.item( i ); if( n.getNodeType() == Node.ATTRIBUTE_NODE ) { Attr a = (Attr)n; if( a.getName().equals( name ) ) return a; } } return null; }

10 DOMの検索プログラムの問題点 検索パターンは複雑 そのすべてに対応するプログラムも複雑 何か手助けはないか?
みんなが利用できる「標準」の方法が望ましい => XPath

11 XPath とは? XML文書のノードの位置を指定 検索の強力な手段 XSLT Xindice

12 XPath の検索パターン(1) abc child::text() child::* preceding-sibling::abc
child::abc abc child::text() child::* preceding-sibling::abc following-sibling::abc attribute::xyz

13 XPath の検索パターン(2) child::*/child::abc ancestor::abc decendant::abc

14 XPath の検索パターン(3) child::abc[position()=1] child::abc[attribute::xyz]

15 XPath の検索パターン(4) abc //abc abc[1] @xyz

16 javax.xml.xpath 実装に依存しない XPathの処理 XPath インターフェイス 検索の処理のメソッドを定義
XPathFactory クラス XPathConstants などのクラス

17 参考:パーサの取得のパターン DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder();

18 XPathの取得 import javax.xml.xpath.*; : XPathFactory xpf =
XPathFactory.newInstance(); XPath xpath = xpf.newXPath();

19 evaluate() メソッドの4パターン 対象となるノード Imputsource(XML文書) Node(文書の一部)
返り値:単純なString or 特定のデータ型(Object)

20 evaluate() メソッドの役割(1) 必要な型のデータを受け取る Document document = db.parse(
new InputSource( "sample3.xml" ); NodeList list = (NodeList)xpath.evaluate( "//ABC" , document, XPathConstants.NODESET );

21 evaluate() メソッドの役割(2) 必要な型のデータを受け取る double d = (Double)xpath.evaluate(
new InputSource( "sample3.xml" ), XPathConstants.NUMBER );


Download ppt "第8回 2007年6月15日 応用Java (Java/XML)."

Similar presentations


Ads by Google