Class DOMWalker


  • public final class DOMWalker
    extends Object
    Document walker class. This class provides an intuitive interface for traversing a parsed DOM document.
    • Constructor Detail

      • DOMWalker

        public DOMWalker​(InputStream xml)
                  throws IOException
        Creates a walker for traversing a DOM document read from the given input stream. The root element of the document is set as the current element.
        Parameters:
        xml - XML input stream
        Throws:
        IOException - if a document cannot be read from the stream
    • Method Detail

      • getNamespaces

        public Properties getNamespaces()
        Returns the namespace mappings defined in the current element. The returned property set contains the prefix to namespace mappings specified by the xmlns attributes of the current element.
        Returns:
        prefix to namespace mappings of the current element
      • getName

        public String getName()
        Returns the name of the current element.
        Returns:
        element name
      • getAttribute

        public String getAttribute​(String name)
        Returns the value of the named attribute of the current element.
        Parameters:
        name - attribute name
        Returns:
        attribute value, or null if not found
      • getContent

        public String getContent()
        Returns the text content of the current element.
        Returns:
        text content
      • enterElement

        public boolean enterElement​(String name)
        Enters the named child element. If the named child element is found, then it is made the current element and true is returned. Otherwise the current element is not changed and false is returned.

        The standard call sequence for this method is show below.

             DOMWalker walker = ...;
             if (walker.enterElement("...")) {
                 ...;
                 walker.leaveElement();
             }
         
        Parameters:
        name - child element name
        Returns:
        true if the element was entered, false otherwise
      • leaveElement

        public void leaveElement()
        Leaves the current element. The parent element is set as the new current element.
        See Also:
        enterElement(String)
      • iterateElements

        public boolean iterateElements​(String name)
        Iterates through the named child elements over multiple calls. This method makes it possible to use the following code to walk through all the child elements with the given name.
             DOMWalker walker = ...;
             while (walker.iterateElements("...")) {
                 ...;
             }
         

        WARNING: This method should only be used when walker.getName() does not equal name when the while loop is started. Otherwise the walker will not be positioned at the same node when the while loop ends.

        Parameters:
        name - name of the iterated elements
        Returns:
        true if another iterated element was entered, or false if no more iterated elements were found and the original element is restored as the current element