Oracle® Fusion Middleware Oracle WebLogic Server XMLのプログラミング 12cリリース1 (12.1.1) B65961-01 |
|
前 |
次 |
この章では、2つの高度なXML API、Java API for XML Registries (JAXR) APIおよびWebLogic XPath APIの使用方法について説明します。
この章の内容は以下のとおりです。
http://java.sun.com/webservices/jaxr/
のJava API for XML Registries (JAXR) APIは、様々なレジストリ、特にWebサービス・アプリケーションで使用されるXMLレジストリにアクセスするための共通の標準Java APIを提供しています。
抽象レベルでは、レジストリは、パートナシップを組んでいる二者の共同作業を助けるニュートラルな第三者です。レジストリは、通常はWebベースのリソースの形式で、共有リソースとして二者が利用できるものです。これにより両者がWebサービスをパブリッシュ、検索、および使用できるようになるので、レジストリは、どのWebサービス・アーキテクチャでも中核となる要素です。
レジストリには、以下の機能があります。
電子的なイエロー・ページ
レジストリは、Webサービス・プロバイダが自社の製品を宣伝し、Webサービス・コンシューマがその製品を検索できる独立したオンライン情報交換サービスを提供します。
レジストリは、業界、地域、販売している製品など、任意かつ柔軟な方法でこれらのWebサービスを分類できます。
比較的静的なデータのデータベース
レジストリは、データベースと同様にデータとメタデータを格納します。格納するデータの種類には、次のものがあります。
特定のビジネス・プロトコルをXMLフォーマットで記述した協調的ビジネス・プロセスの説明。
協調的ビジネス・プロセスに参加している二者。
協調的ビジネス・プロセスの間に交換されるXMLドキュメントを記述するXMLスキーマ。
実質的な側面を言えば、レジストリを使用すると、アプリケーションは、比較的静的なデータを信頼性の高い方法で格納し、そのデータを他のアプリケーションと簡単に共有できます。
また、価格変更や、値引き、販促などの動的なコンテンツを二者間でやり取りする際にレジストリを利用することも可能。
WebLogic Serverでは、JAXR仕様が完全に実装されています。JAXR APIを使用すると、レジストリにアクセスするJAXRクライアントを作成したり、自分でレジストリを作成したりできます。
JAXR APIの使用方法の詳細は、「Java Web Services」のWebサイト(http://java.sun.com/webservices/jaxr/
)を参照してください。
WebLogic XPath APIには、DOM
、XMLInputStream
、またはXMLOutputStream
として表現されるドキュメントに対してXPathのマッチングを実行するために必要となる、すべてのクラスが入っています。このAPIは、指定したパターンに適合するXMLドキュメント内のXML要素のサブセットを識別する場合に使用します。
WebLogic XPath APIのAPIリファレンス情報は、『Oracle WebLogic Server APIリファレンス』のweblogic.xml.xpath
に関する項と、http://java.sun.com/webservices/jaxr/
のJavadocを参照してください。
この節では、DOM
として表現されるXMLドキュメントに対してXPathのマッチングを実行するために、WebLogic XPath APIのDOMXPath
クラスを使用する方法について説明します。最初に例を示し、次にその例で使われている主な手順について説明します。
この節の最後に示すサンプルJavaプログラムでは、次のXMLドキュメントをマッチングに使用しています。
<?xml version='1.0' encoding='us-ascii'?> <!-- "Purchaseorder". --> <purchaseorder department="Sales" date="01-11-2001" raisedby="Pikachu" > <item ID="101"> <title>Laptop</title> <quantity>5</quantity> <make>Dell</make> </item> <item ID="102"> <title>Desktop</title> <quantity>15</quantity> <make>Dell</make> </item> <item ID="103"> <title>Office Software</title> <quantity>10</quantity> <make>Microsoft</make> </item> </purchaseorder>
Javaコード例は次のとおりです。
package examples.xml.xpath; import java.io.IOException; import java.util.Iterator; import java.util.Set; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; import weblogic.xml.xpath.DOMXPath; import weblogic.xml.xpath.XPathException; /** * This class provides a simple example of how to use the DOMXPath * API. * * @author Copyright (c) 2002 by BEA Systems, Inc. All Rights Reserved. */ public abstract class DOMXPathExample { public static void main(String[] ignored) throws XPathException, ParserConfigurationException, SAXException, IOException { // create a dom representation of the document String file = "purchaseorder.xml"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); // doesn't matter for this example DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(file); // create some instances of DOMXPath to evaluate against the // document. DOMXPath totalItems = // count number of items new DOMXPath("count(purchaseorder/item)"); DOMXPath atLeast10 = // titles of items with quantity >= 10 new DOMXPath("purchaseorder/item[quantity >= 10]/title"); // evaluate them against the document double count = totalItems.evaluateAsNumber(doc); Set nodeset = atLeast10.evaluateAsNodeset(doc); // output results System.out.println(file+" contains "+count+" total items."); System.out.println("The following items have quantity >= 10:"); if (nodeset != null) { Iterator i = nodeset.iterator(); while(i.hasNext()) { Node node = (Node)i.next(); System.out.println(" "+node.getNodeName()+ ": "+node.getFirstChild().getNodeValue()); } } // note that at this point we are free to continue using evaluate // atLeast10 and totalItems against other documents } }
次の手順では、DOMXPath
クラスを使用して、DOM
で表現されるXMLドキュメントに対してXPathのマッチングを実行するための主な手順について説明します。
次のサンプル・コードように、XMLドキュメントからorg.w3c.dom.Document
オブジェクトを作成します。
String file = "purchaseorder.xml"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(file);
DOMに対して評価するXPath式を表すDOMXPath
オブジェクトを作成します。
次の例では、発注書内の項目数をカウントするXPath式を示します。
DOMXPath totalItems = new DOMXPath("count(purchaseorder/item)");
次の例では、数量が10以上の項目を持つタイトルを返すXPath式を示します。
DOMXPath atLeast10 = new DOMXPath("purchaseorder/item[quantity >= 10]/title");
DOMXPath.evaluateAs
XXX
()
メソッドの1つを使用して、XPath式を評価します。XXX
は、Boolean
、Nodeset
、Number
、String
といった戻り値のデータ型を表します。
次の例では、evaluateAsNumber()
メソッドを使用してtotalItems
XPath式を評価する方法を示します。
double count = totalItems.evaluateAsNumber(doc); System.out.println(file+" contains "+count+" total items.");
次の例では、evaluateAsNodeset()
メソッドを使用して、標準的な方法で反復処理できるorg.w3c.dom.Nodes
のSet
を返す方法を示します。
Set nodeset = atLeast10.evaluateAsNodeset(doc); System.out.println("The following items have quantity >= 10:"); if (nodeset != null) { Iterator i = nodeset.iterator(); while(i.hasNext()) { Node node = (Node)i.next(); System.out.println(" "+node.getNodeName()+ ": "+node.getFirstChild().getNodeValue()); } }
WebLogic XPath APIのAPIリファレンス情報については、Oracle WebLogic Server APIリファレンスのweblogic.xml.xpath
に関する項を参照してください。
次の例では、WebLogic XPath APIのStreamXPath
クラスを使用して、XMLInputStream
に対してXPathマッチングを実行する方法を示します。最初に例を示し、次にその例で使われている主な手順について説明します。例ではXMLInputStream
に対してマッチングを実行する方法のみを示しますが、同様のコードを使用すればXMLOutputStream
に対してマッチングを実行できます。
この節の最後に示すサンプルJavaプログラムでは、次のXMLドキュメントをマッチングに使用しています。
<?xml version='1.0' encoding='us-ascii'?> <!-- "Stock Quotes". --> <stock_quotes> <stock_quote symbol='BEAS'> <when> <date>01/27/2001</date> <time>3:40PM</time> </when> <price type="ask" value="65.1875"/> <price type="open" value="64.00"/> <price type="dayhigh" value="66.6875"/> <price type="daylow" value="64.75"/> <change>+2.1875</change> <volume>7050200</volume> </stock_quote> <stock_quote symbol='MSFT'> <when> <date>01/27/2001</date> <time>3:40PM</time> </when> <price type="ask" value="55.6875"/> <price type="open" value="50.25"/> <price type="dayhigh" value="56"/> <price type="daylow" value="52.9375"/> <change>+5.25</change> <volume>64282200</volume> </stock_quote> </stock_quotes>
サンプルのJavaコードは次のとおりです。
package examples.xml.xpath; import java.io.File; import weblogic.xml.stream.Attribute; import weblogic.xml.stream.StartElement; import weblogic.xml.stream.XMLEvent; import weblogic.xml.stream.XMLInputStream; import weblogic.xml.stream.XMLInputStreamFactory; import weblogic.xml.stream.XMLStreamException; import weblogic.xml.xpath.StreamXPath; import weblogic.xml.xpath.XPathException; import weblogic.xml.xpath.XPathStreamFactory; import weblogic.xml.xpath.XPathStreamObserver; /** * This class provides a simple example of how to use the StreamXPath * API. * * @author Copyright (c) 2002 by BEA Systems, Inc. All Rights Reserved. */ public abstract class StreamXPathExample { public static void main(String[] ignored) throws XPathException, XMLStreamException { // Create instances of StreamXPath for two xpaths we want to match // against this tream. StreamXPath symbols = new StreamXPath("stock_quotes/stock_quote"); StreamXPath openingPrices = new StreamXPath("stock_quotes/stock_quote/price[@type='open']"); // Create an XPathStreamFactory. XPathStreamFactory factory = new XPathStreamFactory(); // Create and install two XPathStreamObservers. In this case, we // just use to anonymous classes to print a message when a // callback is received. Note that a given observer can observe // more than one xpath, and a given xpath can be observed by // mutliple observers. factory.install(symbols, new XPathStreamObserver () { public void observe(XMLEvent event) { System.out.println("Matched a quote: "+event); } public void observeAttribute(StartElement e, Attribute a) {} //ignore public void observeNamespace(StartElement e, Attribute a) {} //ignore }); // Note that we get matches for both a start and an end elements, // even in the case of <price/> which is an empty element - this // is the behavior of the underlying streaming parser. factory.install(openingPrices, new XPathStreamObserver () { public void observe(XMLEvent event) { System.out.println("Matched an opening price: "+event); } public void observeAttribute(StartElement e, Attribute a) {} //ignore public void observeNamespace(StartElement e, Attribute a) {} //ignore }); // get an XMLInputStream on the document String file = "stocks.xml"; XMLInputStream sourceStream = XMLInputStreamFactory.newInstance(). newInputStream(new File(file)); // use the factory to create an XMLInputStream that will do xpath // matching against the source stream XMLInputStream matchingStream = factory.createStream(sourceStream); // now iterate through the stream System.out.println("Matching against xml stream from "+file); while(matchingStream.hasNext()) { // we don't do anything with the events in our example - the // XPathStreamObserver instances that we installed in the // factory will get callbacks for appropriate events XMLEvent event = matchingStream.next(); } } }
次の手順では、StreamXPath
クラスを使用して、XMLInputStream
として表現されるXMLドキュメントに対してXPathのマッチングを実行するための主な手順を説明します。
XMLInputStream
に対して評価するXPath式を表すStreamXPath
オブジェクトを作成します。
StreamXPath symbols = new StreamXPath("stock_quotes/stock_quote");
次の例では、開始値を使用して株価を検索するXPath式を示します。
StreamXPath openingPrices = new StreamXPath("stock_quotes/stock_quote/price[@type='open']");
XPathStreamFactory
を作成します。このファクトリ・クラスを使用して、XMLInputStream
に対して評価するStreamXPath
オブジェクトのセットを指定し、XPathに一致するものが見つかった場合にコールバックを登録するためのオブザーバ(XPathStreamObserver
インタフェースを使用する)を作成します。次の例では、XPathStreamFactory
の作成方法を示します。
XPathStreamFactory factory = new XPathStreamFactory();
XPathStreamFactory.install()
メソッドを使用し、1番目のStreamXPath
パラメータでXPath式を、2番目のXPathStreamObserver
パラメータでオブザーバを指定して、オブザーバを作成およびインストールします。次の例は、無名クラスを使用してXPathStreamObserver
インタフェースを実装する方法を示します。observe()
メソッドの実装は、コールバックを受け取ったときにメッセージの出力だけを行います。例では、observeAttribute()
メソッドとobserveNamespace()
メソッドは何もしません。
factory.install(symbols, new XPathStreamObserver () { public void observe(XMLEvent event) { System.out.println("Matched a quote: "+event); } public void observeAttribute(StartElement e, Attribute a) {} public void observeNamespace(StartElement e, Attribute a) {} } );
XMLドキュメントからXMLInputStream
を作成します。
String file = "stocks.xml"; XMLInputStream sourceStream = XMLInputStreamFactory.newInstance().newInputStream(new File(file));
XPathStreamFactory
のcreateStream()
メソッドを使用して、元のXMLInputStream
に対してXPathのマッチングを実行する新しいXMLInputStream
を作成します。
XMLInputStream matchingStream = factory.createStream(sourceStream);
新しいXMLInputStreamを反復処理します。反復処理中にXPathに一致するものが見つかった場合、登録済みのオブザーバに通知されます。
while(matchingStream.hasNext()) { XMLEvent event = matchingStream.next(); }
WebLogic XPath APIのAPIリファレンス情報については、Oracle WebLogic Server APIリファレンスのweblogic.xml.xpath
に関する項を参照してください。