> -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of > [EMAIL PROTECTED] > Sent: Wednesday 16 June 2004 20:26 > To: [EMAIL PROTECTED] > Subject: Re: [dom4j-user] Removing Nodes > > > On Wednesday 16 June 2004 19:54, Mike Skells wrote: > > this bugin the dev newsgroup > > newsgroup? [EMAIL PROTECTED] > > > if(n.getName().equalsIgnoreCase("div")) > > { > > n.detach(); //better than n.getParent().remove(n) as getParent() > > may be null > > } > > But this one throws a NullPointerException (if Element) or > just doesn't work > respectively (if Node)...or did I misunderstand you? This will not throw a NPE. The NPE gets throw ONLY in the visitor code when parent node has content removed. Have a look at the source, and you can see the problem. I have tested it befor I sent it :-)
If you want to use the visitor pattern, you can avoid the iteration problem by iterating across the child nodes in the element visitor, like this static class KillDivs extends VisitorSupport { public void visit( Element e ) { List content = e.elements(); for (int i = content.size()-1; i >= 0 ; i--) { Element child = (Element)content.get(i); if (child.getName().equalsIgnoreCase("div")) { content.remove(i); } } } } Mike > > > ------------------------------------------------------- > This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference > Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer > Conference, June 28 - July 1 at the Moscone Center in San > Francisco, CA > REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority > Code NWMGYKND > _______________________________________________ > dom4j-user mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/dom4j-user > ------------------------------------------------------- This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user