[jdom-interest] traversing the JDOM tree

Odd Moller odd.moller at cypoint.se
Wed Apr 16 01:16:04 PDT 2003


Fixed a little bug in my previous code, and added tree traversal.
Hope it helps.


/**
  * Returns a list of all elements that are descendents of the specified
element.
  * If no elements are found an empty list is returned.
  */ 
public static List getSubElements(Element element) {
   List subElements = new ArrayList();
   addSubElements(element, null, subElements);
   return subElements;
}

/**
  * Returns a list of all elements that are descendents of the specified
element,
  * and that has a name equal to the specified name.
  * If no elements are found an empty list is returned.
  */ 
public static List getSubElements(Element element, String name) {
   List subElements = new ArrayList();
   addSubElements(element, name, subElements);
   return subElements;
}

private static void addSubElements(Element element, String name, List
elements) {
   for (Iterator i = element.getChildren().iterator(); i.hasNext();) {
      Element subElement = (Element)i.next();
      if (name != null && name.equals(subElement.getName())) {
         elements.add(subElement);
      }
      addSubElements(subElement, name, elements);
   }
}

public interface Operation {
   void operate(Element element);
}

/**
  * Traverses and applies the specified operation to the tree of elements
that
  * are descendents of the specified element. If a name is specified, only
  * elements that match that name are operated upon.
  *
  * @param element The root element of the traversal.
  * @param name If non null, only elements with a name equal to it are
operated upon.
  * @param operation The operation to apply.
  */
public static void traverseElementTree(Element element, String name,
Operation operation) {
   if (name != null && name.equals(element.getName())) {
      operation.operate(element);
   }
   List children = (name == null) ? element.getChildren() :
element.getChildren(name);
   for (Iterator i = children.iterator(); i.hasNext();) {
      Element subElement = (Element)i.next();
      traverseElementTree(subElement, name, operation);
   }
}

///Odd Moller 


-----Original Message----- 
From: Vivek Kapadekar [mailto:vkapadekar at bitfone.com] 
Sent: den 15 april 2003 18:59 
To: jdom-interest at jdom.org 
Subject: [jdom-interest] traversing the JDOM tree 



hi 
I am using JDOM for the first time. It looks really cool, but one 
functionality that I am looking for which is missing in it. And that is 
for traversing the JDOM tree. 
The getChild and getChildren gets only the elements one level deep. What 
If i want to recursively find and Element or check existence of an 
element, like getElementByName() or findElement(). 
Thanks 
--Vivek 


_______________________________________________ 
To control your jdom-interest membership: 
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20030416/b7fec22f/attachment.htm


More information about the jdom-interest mailing list