Ronald
Ronald

Reputation: 2917

how to delete node children?

before I write may sitemap.xml in some jcr folder /var/myProject/sitemap, I would like to delete the old sitemap.xml

Resource resource = resourceResolver.getResource("/var/myProject/sitemap");
sitemapNode = resource.adaptTo(Node.class);
sitemapNode.getNodes().remove();

sitemapNode.getNodes().remove(); deliver the following exception:

*ERROR* [CM Event Dispatcher (Fire ConfigurationEvent: pid=MyClass] The start method has thrown an exception (java.lang.UnsupportedOperationException)

How to remove all children of the sitemapNode ?

Upvotes: 0

Views: 6881

Answers (1)

rakhi4110
rakhi4110

Reputation: 9281

The sitemapNode.getNodes(); returns a NodeIterator. The remove() method available in that is the one inherited from Iterator.

Quoting the docs remove()

Removes from the underlying collection the last element returned by this iterator (optional operation)

When you try to remove from an unmodifiable collection, it gives the UnsupportedOperationException.

Either remove the sitemapNode using sitemapNode.remove() and recreate it (this would remove all its children along with the node), or iterate through each child and then call the remove() method individually. This would invoke the Node's remove() method inherited from javax.jcr.Item.

Upvotes: 1

Related Questions