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 boolean
enterElement(String name)
Enters the named child element.String
getAttribute(String name)
Returns the value of the named attribute of the current element.String
getContent()
Returns the text content of the current element.String
getName()
Returns the name of the current element.Properties
getNamespaces()
Returns the namespace mappings defined in the current element.boolean
iterateElements(String name)
Iterates through the named child elements over multiple calls.void
leaveElement()
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 thexmlns
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 andtrue
is returned. Otherwise the current element is not changed andfalse
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 equalname
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, orfalse
if no more iterated elements were found and the original element is restored as the current element
-
-