|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.greybird.xmliter.DomIterator
An XmlIterator that obtains its data from a DOM tree. This class is provided so that the XmlIterator interface may be used with a DOM when one is available.
DomIterator adds less than 3% processing overhead compared to direct use of the DOM API. This percentage is relative to the total time to parse and traverse a document. It comes from the PerformanceTest included with this package.
To use a DomIterator, first obtain a DOM Node that is the parent of the elements and text to be traversed. Typically this is a Document node, but it could also be an Element or a DocumentFragment. Then construct the DomIterator specifying the parent node to be used.
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.greybird.xmliter.DomIterator; import org.greybird.xmliter.XmlIterator; import org.w3c.dom.Node; ... // // Parse a document to obtain a Document node. // DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Node document = builder.parse(input); // // Now use the document to construct a DomIterator. // XmlIterator iter = new DomIterator(document); while (iter.advance()) { ...
Constructor Summary | |
DomIterator(org.w3c.dom.Node parentNode)
Creates a DOM iterator using a given parent node. |
Method Summary | |
boolean |
advance()
Moves to the next element at the current level and returns true, or returns false if there are no more elements. |
boolean |
advanceMixed()
Moves to the next element or text item at the current level and returns true, or returns false if there are no more items. |
java.lang.String |
attribute(java.lang.String namespace,
java.lang.String name)
Returns the value of the attribute with the given name belonging to the current element, or null if no such attribute exists, or null if positioned on a mixed text item. |
XmlAttributes |
attributes()
Returns an iterator over the attributes belonging to the current element, or null if positioned on a mixed text item. |
XmlIterator |
children()
Returns an iterator positioned before the first child of the current element, or null if the current element does not contain any element children, or null if positioned on a mixed text item. |
java.lang.String |
name()
Returns the local name of the current element, or null if positioned on a mixed text item. |
java.lang.String |
namespace()
Returns the namespace URI of the current element, or an empty string if the current element has no namespace, or null if positioned on a mixed text item. |
java.lang.String |
toString()
Returns a string for error reporting that identifies the current element. |
java.lang.String |
value()
Returns the text contained by the current element or text item, or null if positioned on an element that contains one or more element children. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public DomIterator(org.w3c.dom.Node parentNode)
Method Detail |
public boolean advance() throws java.lang.IllegalStateException, java.io.IOException, XmlIteratorException
XmlIterator
advance
in interface XmlIterator
org.greybird.xmliter.XmlIterator
java.lang.IllegalStateException
- if an element would be returned out of
depth-first tree order.java.io.IOException
- if an error occurs retrieving input data.XmlIteratorException
- if an error occurs processing input data.public boolean advanceMixed() throws java.lang.IllegalStateException, java.io.IOException, XmlIteratorException
XmlIterator
advanceMixed
in interface XmlIterator
org.greybird.xmliter.XmlIterator
java.lang.IllegalStateException
- if an element or text item would be
returned out of depth-first tree order.java.io.IOException
- if an error occurs retrieving input data.XmlIteratorException
- if an error occurs processing input data.public java.lang.String name() throws java.lang.IllegalStateException
XmlIterator
name
in interface XmlIterator
org.greybird.xmliter.XmlIterator
java.lang.IllegalStateException
- if advance() or advanceMixed() has not yet
been called.public java.lang.String namespace() throws java.lang.IllegalStateException
XmlIterator
namespace
in interface XmlIterator
org.greybird.xmliter.XmlIterator
java.lang.IllegalStateException
- if advance() or advanceMixed() has not yet
been called.public java.lang.String attribute(java.lang.String namespace, java.lang.String name) throws java.lang.IllegalStateException, java.io.IOException, XmlIteratorException
XmlIterator
WARNING: Unlike the DOM methods Node.getAttribute() and getAttributeNS() this method returns null, not an empty string, when the attribute does not exist.
attribute
in interface XmlIterator
org.greybird.xmliter.XmlIterator
namespace
- is the namespace of the attribute or may be null or
an empty string if the attribute has the empty namespace.name
- is the local name of the attribute.java.lang.IllegalStateException
- if advance() or advanceMixed() has not yet
been called.java.io.IOException
- if an error occurs retrieving input data.XmlIteratorException
- if an error occurs processing input data.public XmlAttributes attributes() throws java.lang.IllegalStateException, java.io.IOException, XmlIteratorException
XmlIterator
attributes
in interface XmlIterator
org.greybird.xmliter.XmlIterator
java.lang.IllegalStateException
- if advance() or advanceMixed() has not yet
been called.java.io.IOException
- if an error occurs retrieving input data.XmlIteratorException
- if an error occurs processing input data.public java.lang.String value() throws java.lang.IllegalStateException, java.io.IOException, XmlIteratorException
XmlIterator
This method always returns the complete text between elements, which may be the result of concatenating multiple SAX character events or DOM text nodes.
value
in interface XmlIterator
org.greybird.xmliter.XmlIterator
java.lang.IllegalStateException
- if advance() or advanceMixed() has not yet
been called.java.io.IOException
- if an error occurs retrieving input data.XmlIteratorException
- if an error occurs processing input data.public XmlIterator children() throws java.lang.IllegalStateException, java.io.IOException, XmlIteratorException
XmlIterator
If this method is called for an element more than once it will return the same iterator instance as was returned the first time it was called, and therefore the returned iterator may no longer be positioned before its first child.
children
in interface XmlIterator
org.greybird.xmliter.XmlIterator
java.lang.IllegalStateException
- if advance() or advanceMixed() has not yet
been called.java.io.IOException
- if an error occurs retrieving input data.XmlIteratorException
- if an error occurs processing input data.public java.lang.String toString()
XmlIterator
[/ns:TopElement/ns:ChildElement]
toString
in interface XmlIterator
toString
in class java.lang.Object
|
Copyright (c) 2003 Mark T. Hayes; All Rights Reserved | |||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |