[jdom-interest] Re: replacing elements

Todd O'Bryan toddobryan at mac.com
Mon Aug 20 12:38:33 PDT 2001


Whoops! Avoid writing code without consulting the javadocs...

Because getChildren() returns just the Element children, my check to see 
if a child is instanceof an Element is unnecessary.

Also, I should have mentioned that you should pass the root element of a 
document you wish to change to this method and the whole document should 
be fixed.

Here's the method as it should have read (with some comments clarified 
and extraneous code removed):

// replaces all <a> elements with <link> elements at and nested within 
the Element passed in
void replaceAsWithLinks(Element e) {
	//replace this element if needed
	if (e.getName().equals("a")) {
		e.setName("link");
	}
	//now check its children
	List l = e.getChildren();
	for (int i=0; i<l.getSize(); i++) {
		replaceAsWithLinks( (Element) l.get(i));
	}
}



More information about the jdom-interest mailing list