Navegación con XTreeCursor

Puede utilizar la interfaz XTreeCursor para ver los datos.

Acerca de esta tarea

Supongamos que la secuencia que se obtiene de la evaluación de una expresión XPath o XQuery o una hoja de estilo XSLT contiene nodos. Le parecería muy aconsejable acceder al contenido de estos nodos aplicando más expresiones XPath o XQuery a estos nodos. Sin embargo, también es posible que decida navegar por la estructura en árbol asociada a un nodo directamente a través de la API XML.

Procedimiento

Ejemplo

El ejemplo siguiente muestra cómo se pueden utilizar los métodos de la interfaz XTreeCursor para navegar a través del árbol que contiene los datos XML.

/* Contents of library.xml

<library>
  <book title='Ulysses'><author><first>James</first><last>Joyce</last></author></book>
  <book title='Ada'><author><first>Vladimir</first><last>Nabokov</last></author></book>
</library>
*/

XItemFactory factory = XFactory.newInstance().getItemFactory();
XItemView item = factory.item(new StreamSource("library.xml"));

// 'tree' is initially positioned at the same node as
// 'item' - that is, the document node of the input
XTreeCursor tree = item.getTreeCursor();

// Position tree cursor to "library" element
tree.toFirstChild();

// Position tree cursor to white-space text node
tree.toFirstChild();

// Position to first "book" element
tree.toNextSibling();

// Position to white-space text node
tree.toNextSibling();

// Position to second "book" element
tree.toNextSibling();

// Create a second instance of XTreeCursor that is initially
// positioned at the same node as 'tree' - that is, the
// second "book" element
XTreeCursor secondBook = tree.getTreeCursor();

// Position 'tree' to "library" element
tree.toParent();

// Position 'secondBook' to "title" attribute
secondBook.toFirstAttribute();

El ejemplo siguiente navega por todos los nodos de un árbol de una forma poco profunda.

XItemFactory factory = XFactory.newInstance().getItemFactory();
XItemView item = factory.item(new StreamSource("library.xml"));

XTreeCursor tree = item.getTreeCursor();
boolean hasMoreNodes = true;

do {
    // Process current node

    if (tree.toFirstAttribute()) {
        do {
            // Process attributes
        } while (tree.toNextAttribute());
        tree.toParent();
    }

    if (tree.toFirstNamespace()) {
        do {
            // Process namespaces
        } while (tree.toFirstNamespace());
        tree.toParent();
    }

    boolean foundNext = false;

    // If the current node has a child, visit it next
    // If there's no child, go to the next sibling
    if (tree.toFirstChild() || tree.toNextSibling()) {
        foundNext = true;

    // If there's no child and no sibling, find an
    // ancestor's sibling instead.
    } else {
        do {
            hasMoreNodes = tree.toParent();
            if (hasMoreNodes) {
                foundNext = tree.toNextSibling();
            }
        } while (hasMoreNodes && !foundNext);
    }
} while (hasMoreNodes);

Icon that indicates the type of topic Task topic



Timestamp icon Last updated: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=txml_nav_xtree
File name: txml_nav_xtree.html