使用 XSequenceCursor 导航
XSequenceCursor 接口为您提供序列数据的视图。
关于此任务
XPath 和 XQuery 数据模型中的序列包含零个或多个原子值、节点或这两者的组合。XSequenceCursor 接口的实例始终至少包含一个项。 如果序列为空,那么它始终由空引用表示。
过程
- 在任何给定时间,对 XSequenceCursor 接口的实例进行定位,从而为您提供对序列中其中一个项的访问权。
最初,将其定位为提供序列中第一个项的访问权;您可以分别使用 toNext() 或 toPrevious() 方法,将序列中的项前移或者后移,且一次移动一个项。
如果 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()
以获取 XItemView 接口的实例,此接口包含与 XSequenceCursor 接口的实例中当前项关联的数据。
如果通过 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