java 使用 removeContent() 从 JDOM 文档中删除元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5634003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:00:56  来源:igfitidea点击:

Remove Element from JDOM document using removeContent()

javaxmljdom

提问by Swift-Tuttle

Given the following scenario, where the xml, Geography.xml looks like -

鉴于以下场景,其中 xml,Geography.xml 看起来像 -

<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 -

以及以下示例 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);

The removeContent()method doesn't remove the Element Cityfrom 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& Populationelements 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-

removeContent()方法不会Citydoc. b 的值是false
我不明白为什么它不删除元素,我什至试图从 xml 中删除Name&Population元素只是为了看看这是否是问题,但显然不是。
我尝试的另一种方法,我不知道为什么我知道它没有本质上的不同,仍然只是为了这个目的,是使用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.

可能是什么问题?和可能的解决方案?如果有人可以分享该方法的真实行为removeContent(),我怀疑这与父子关系有关。

回答by forty-two

Sure, removeContent(Content child)removes child if child belongs to the parents immediate children, which it does not in your case. Use el.detach()instead.

当然,removeContent(Content child)如果孩子属于父母的直系孩子,则删除孩子,而在您的情况下则不然。使用el.detach()来代替。

回答by dogbane

If you want to remove the Cityelement, get its parent and call removeContent:

如果要删除City元素,请获取其父元素并调用removeContent

    XPath xpath = XPath.newInstance("/*/Country/Region/State/City");
    Element el = (Element) xpath.selectSingleNode(doc);
    el.getParent().removeContent(el);

The reason why doc.removeContent(el)does not work is because elis not a child of doc.

之所以doc.removeContent(el)不起作用,是因为el不是doc.

Check the javadocsfor details. There are a number of overloaded removeContentmethods there.

查看javadoc了解详细信息。那里有许多重载removeContent方法。

回答by Dan Ortega

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.

这种方式的工作牢记 .getParent() 返回一个 Parent 对象而不是一个 Element 对象,并且必须从 Element 调用消除实际节点的 detach() 方法。

Instead do:

而是这样做:

el.getParentElement().detach();

This will remove the parent element with all it's children !

这将删除父元素及其所有子元素!