Jump to content

Java: Remove all Elements from the Document Element


msphelix

Recommended Posts

Hi, I am displaying Nodes from the following XML file in a Java GUI.<?xml version="1.0" encoding="ISO-8859-1"?><bookstore> <book category="cooking"> <title>Everyday Italian</title> <author>Giada De Laurentiis</author> </book><!-- more book elements --></bookstore>I haveElement documentElement = xmlDocument.getDocumentElement();andNodeList bookNodes = documentElement.getElementsByTagName("book");I want to allow the user to remove all <book> elements while not destroying xmlDocument - I want the user to be able to add <book> elements to the same Document even after removing all of the existing elements.I can do this by running bookNodes from tail to head, getting each element, and calling documentElement.removeChild(thisElement).ButIs there a way to do this in one call?documentElement.removeAll();Thanks.

Link to comment
Share on other sites

When you want to keep some stuff... no. If you want to completely clean the bookstore element, you could just use replaceChild over the element itself, i.e.

xmlDocument.replaceChild(documentElement.cloneNode(false), documentElement);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...