Jump to content

Java: Remove all Elements from the Document Element


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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...