使用removeContent()从JDOM文档中删除元素
给定以下场景,其中 xml、Geography.xml 看起来像 -
<Geography xmlns:ns="some valid namespace">
<Country>
<Region>
<State>
<City>
<Name></Name>
<Population></Population>
</City>
</State>
</Region>
</Country>
</Geography>
以及以下示例 java 代码 -
InputStream is = new FileInputStream("C:\\Geography.xml");
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = saxBuilder.build(is);
XPath xpath = XPath.newInstance("/*/Country/Region/State/City");
Element el = (Element) xpath.selectSingleNode(doc);
boolean b = doc.removeContent(el);
removeContent()
方法不会从doc
的内容列表。 b 的值为 false
我不明白为什么它不删除元素,我什至尝试删除 Name
& xml 中的 Pop
元素只是为了看看这是否是问题所在,但显然不是。
我尝试的另一种方法,我不知道为什么我知道它没有本质上的不同,仍然只是为了使用 Parent
-
Parent p = el.getParent();
boolean s = p.removeContent(new Element("City"));
可能会出现什么问题?以及可能的解决方案?如果有人可以分享方法 removeContent()
的真实行为,我怀疑它与父子关系有关。
Given the following scenario, where the xml, Geography.xml looks like -
<Geography xmlns:ns="some valid namespace">
<Country>
<Region>
<State>
<City>
<Name></Name>
<Population></Population>
</City>
</State>
</Region>
</Country>
</Geography>
and the following sample java code -
InputStream is = new FileInputStream("C:\\Geography.xml");
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = saxBuilder.build(is);
XPath xpath = XPath.newInstance("/*/Country/Region/State/City");
Element el = (Element) xpath.selectSingleNode(doc);
boolean b = doc.removeContent(el);
The removeContent()
method doesn't remove the Element City
from the content list of the doc
. The value of b is false
I don't understand why is it not removing the Element, I even tried to delete the Name
& Population
elements from the xml just to see if that was the issue but apparently its not.
Another way I tried, I don't know why I know its not essentially different, still just for the sake, was to use Parent
-
Parent p = el.getParent();
boolean s = p.removeContent(new Element("City"));
What might the problem? and a possible solution? and if anyone can share the real behaviour of the method removeContent()
, I suspect it has to do with the parent-child relationship.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然,如果孩子属于父母的直接孩子,则
removeContent(Content child)
会删除孩子,但在您的情况下则不然。请改用 el.detach() 。Sure,
removeContent(Content child)
removes child if child belongs to the parents immediate children, which it does not in your case. Useel.detach()
instead.如果要删除
City
元素,请获取其父元素并调用removeContent
:doc.removeContent(el)
不起作用的原因是因为el
不是doc
的子级。有关详细信息,请查看 javadocs。其中有许多重载的
removeContent
方法。If you want to remove the
City
element, get its parent and callremoveContent
:The reason why
doc.removeContent(el)
does not work is becauseel
is not a child ofdoc
.Check the javadocs for details. There are a number of overloaded
removeContent
methods there.这种方法的工作原理是记住 .getParent() 返回一个 Parent 对象而不是 Element 对象,并且必须从 Element 调用消除实际节点的 detach() 方法。
相反,这样做:
这将删除父元素及其所有子元素!
This way works keeping in mind that .getParent() returns a Parent object instead of an Element object, and the detach() method which eliminates the actual node, must be called from an Element.
Instead do:
This will remove the parent element with all it's children !