Package org.apache.jackrabbit.core.util
Class DOMWalker
- java.lang.Object
-
- org.apache.jackrabbit.core.util.DOMWalker
-
public final class DOMWalker extends Object
Document walker class. This class provides an intuitive interface for traversing a parsed DOM document.
-
-
Constructor Summary
Constructors Constructor Description DOMWalker(InputStream xml)Creates a walker for traversing a DOM document read from the given input stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanenterElement(String name)Enters the named child element.StringgetAttribute(String name)Returns the value of the named attribute of the current element.StringgetContent()Returns the text content of the current element.StringgetName()Returns the name of the current element.PropertiesgetNamespaces()Returns the namespace mappings defined in the current element.booleaniterateElements(String name)Iterates through the named child elements over multiple calls.voidleaveElement()Leaves the current element.
-
-
-
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 thexmlnsattributes 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
nullif 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 andtrueis returned. Otherwise the current element is not changed andfalseis returned.The standard call sequence for this method is show below.
DOMWalker walker = ...; if (walker.enterElement("...")) { ...; walker.leaveElement(); }- Parameters:
name- child element name- Returns:
trueif the element was entered,falseotherwise
-
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 equalnamewhen 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:
trueif another iterated element was entered, orfalseif no more iterated elements were found and the original element is restored as the current element
-
-