XSequenceCursor로 탐색
XSequenceCursor 인터페이스에서는 시퀀스 데이터 보기를 제공합니다.
이 태스크 정보
XPath 및 XQuery 데이터 모델의 시퀀스는 0개 이상의 원자 값, 노드 또는 둘의 혼합을 포함합니다. 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() 메소드를 호출하여
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