1 / 21

応用 Java(Java/XML) 第 8 回

応用 Java(Java/XML) 第 8 回. 2005 年 6 月 9 日 植田龍男. 前回の復習. DOM の応用 DOM ツリー内の検索処理 DOM ツリーの編集 ツリーの視覚化 (Swing 使用 ). 検索処理の例(要素名の検索). public void search( Node node ) { if( node.getNodeName().equals( “ A ” ) ) print( node ); NodeList list = node.getChildNodes();

Télécharger la présentation

応用 Java(Java/XML) 第 8 回

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 応用Java(Java/XML) 第8回 2005年6月9日 植田龍男

  2. 前回の復習 • DOMの応用 • DOMツリー内の検索処理 • DOMツリーの編集 • ツリーの視覚化 (Swing使用)

  3. 検索処理の例(要素名の検索) 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)); } }

  4. 宿題の答え(属性の検索 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 ); } }

  5. 宿題の答え(属性の検索 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; }

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

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

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

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

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

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

  12. J2SE 5.0 と XML API • javax.xml • javax.xml.parsers • javax.xml.transform • javax.xml.transform.dom • javax.xml.transform.sax • javax.xml.transform.stream • javax.xml.xpath • javax.xml.namespace • javax.xml.validation • javax.xml.datatype

  13. javax.xml の思想 • 多数のパーサの実装を統合する枠組み • パーサの実装に非依存なアプリケーション • 実現のために抽象化されたクラス群を定義 • 共通の「デザインパターン」

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

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

  16. 実装に依存しないXPathの取得 import javax.xml.xpath.*; : XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); :

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

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

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

  20. ここまでやってきたこと • XMLパーサ(parser)の概念 • パーサの規格(SAX,DOM)

  21. これからの目標 • XPath • XSLT • 名前空間(Namespace) • XML文書の妥当性の検証 DTD,XML Schema

More Related