XSequenceCursor によるナビゲート
XSequenceCursor インターフェースにより、シーケンス・データを表示することができます。
このタスクについて
XPath および XQuery データ・モデルのシーケンスには、 ゼロ以上のアトミック値またはノード、あるいはこれらを混合したものが含まれます。XSequenceCursor インターフェースのインスタンスには、常に 1 つ以上の項目が含まれます。 シーケンスが空の場合は、常にヌル参照で表されます。
手順
- 常に、XSequenceCursor インターフェースのインスタンスは、シーケンス内の
項目のいずれかにアクセスできるように位置付けられています。
このインスタンスは、初めはシーケンス内の最初の項目に位置付けられています。
toNext() メソッドまたは toPrevious() メソッドを個々に使用することで、項目を一度に 1 つずつ、
シーケンス内を前後に移動することができます。
toNext() メソッドまたは toPrevious() メソッドがシーケンス内の次の項目または 前の項目に XSequenceCursor インスタンスを位置付けることができる場合、つまり 実際に次の項目や前の項目が存在する場合、メソッドは true を返します。 XSequenceCursor インスタンスが既に、toNext() メソッドの場合は最後の項目に、 toPrevious() メソッドの場合は最初の項目に位置付けられている場合、 メソッドは false を返し、XSequenceCursor インターフェースのインスタンスは、 メソッドを呼び出す前と同じ項目に位置付けられたままになります。
XSequenceCursor インターフェースのインスタンスに含まれる シーケンスの標準的な処理方法は、以下の例に示すとおりです。XFactory factory = XFactory.newInstance(); XPathExecutable expr = factory.prepareXPath("1 to 10"); XSequenceCursor exprResult = expr.execute(); long sum = 0; // If exprResult is null, it means the result sequence is empty if (exprResult != null) { do { // Get each value as a primitive Java long value, and accumulate sum = sum + exprResult.getLongValue(); // Advance exprResult to the next item in the sequence } while (exprResult.toNext()); } System.out.println("Sum is " + sum);
XSequenceCursor インターフェースは XItemView インターフェースを拡張しています。XItemView インターフェースから継承されたメソッドを使用すると、XSequenceCursor が現在位置付けられているシーケンス内の項目の値および型にアクセスできます。
- アプリケーションでシーケンス内の複数の項目を同時に参照する場合は、
XSequenceCursor.getSingletonItem() メソッドを呼び出して、XSequenceCursor インターフェースの
インスタンス内にある現在の項目に関連付けられたデータを含む XItemView インターフェースのインスタンスを
取得できます。
toNext() メソッドまたは toPrevious() メソッドへの呼び出しを通じて XSequenceCursor インターフェースの 当該インスタンスの位置を変更しても、XSequenceCursor.getSingletonItem() メソッドへの初期の呼び出しで返された XItemView インターフェースの インスタンスはそのままその初期の項目を参照することになります。
あるシーケンス内で、最初の項目と同じ型と値をもつ項目の数をカウントする例を考えてみます。
変数 firstItem は XSequenceCursor.getSingletonItem() メソッドによって作成されるため、常にシーケンス内の最初の項目を参照します。 変数 currentItem には XSequenceCursor オブジェクトへの参照が含まれているため、 この変数は常にシーケンス内の現在の項目に位置付けられます。XFactory factory = XFactory.newInstance(); // Make a path expression whose result contains ordered part number as first item // and all part numbers used by products in the catalog as the subsequent items XPathExecutable expr = factory.prepareXPath( "string(/order/item/@part-num),doc('catalog.xml')/catalog/product/part/string(@part-num)"); // Read the invoice file XSequenceCursor exprResult = expr.execute(new StreamSource(invoiceFile)); int sameAsFirstCount = 0; // If exprResult is null, it means the result sequence is empty if (exprResult != null) { // Get the first item in the result sequence XItemView firstItem = exprResult.getSingletonItem(); // currentItem always refers to the current item in the result sequence XItemView currentItem = exprResult; do { // Get the type of the first item XTypeConstants.Type itemType = firstItem.getValueType(); // Ensure the type of the first item is the same as the type of // the current item, and compare their values as Java objects if (itemType == currentItem.getValueType() && firstItem.getObjectValue(itemType) .equals(currentItem.getObjectValue(itemType))) { sameAsFirstCount++; } //Advance exprResult (and currentItem) to the next item in the sequence } while (exprResult.toNext()); } System.out.println("Number of items same as the first == "+(sameAsFirstCount-1));
- シーケンス内の項目への非順次アクセスを必ず行う必要がある場合は、
XSequenceCursor インターフェースで exportAsList メソッドを使用すると便利でしょう。
このメソッドは、独自のシーケンス内の項目を含めた、java.util.List<XItemView> インターフェースの インスタンスをそのシーケンス順で返します。


http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=txml_nav_xseq
ファイル名:txml_nav_xseq.html