如何删除节点子节点?

how to delete node children?

在我写 may sitemap.xml 在一些 jcr 文件夹 /var/myProject/sitemap 之前,我想删除旧的 sitemap.xml

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

sitemapNode.getNodes().remove(); 传递以下异常:

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

如何删除站点地图节点的所有子节点?

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

引用文档remove()

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

当您尝试从不可修改的集合中删除时,它会给出 UnsupportedOperationException

使用 sitemapNode.remove() 删除 sitemapNode 并重新创建它(这将删除它的所有子节点以及节点),或者遍历每个子节点然后分别调用 remove() 方法。这将调用从 javax.jcr.Item.

继承的节点的 remove() 方法