001 import org.w3c.dom.Element;
002 import org.w3c.dom.Node;
003
004 /**
005 *
006 * ChildElementWalker provides a convenience class used to
007 * walk through all childs of an element that are elements
008 * and to skip text nodes that are assumed to be white space
009 *
010 * @author Udo Altmann
011 * @version 1.0
012 */
013 public class ChildElementWalker {
014
015 Element parent;
016 Node current;
017 String tagname;
018
019 /**
020 * @param p parent element
021 * @param t null (every element is considered) or name of specific elements
022 *
023 */
024 ChildElementWalker(Element p, String t) {
025 parent = p;
026 tagname = t;
027 reset();
028 }
029
030 /**
031 * resets to first child
032 */
033 public void reset() {
034 if( parent != null ) {
035 current = parent.getFirstChild();
036 } else {
037 current = null;
038 }
039 }
040
041
042 /**
043 *
044 * @return next element found or null if there is no more element
045 */
046 public Element getNextElement() {
047 Element rueckgabe = null;
048 while(current != null) {
049 if(current.getNodeType() == Node.ELEMENT_NODE) {
050 if(tagname == null) {
051 rueckgabe = (Element) current;
052 } else {
053 if(current.getNodeName().equals(tagname) || tagname.equals("*") ) {
054 rueckgabe = (Element) current;
055 }
056 }
057 }
058 current = current.getNextSibling();
059 if(rueckgabe != null) {
060 return rueckgabe;
061 }
062 }
063 return null;
064 }
065
066
067 }