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

发布于 2024-10-31 12:06:31 字数 1239 浏览 4 评论 0原文

给定以下场景,其中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对不⑦ 2024-11-07 12:06:31

当然,如果孩子属于父母的直接孩子,则 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. Use el.detach()instead.

︶葆Ⅱㄣ 2024-11-07 12:06:31

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

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

doc.removeContent(el) 不起作用的原因是因为 el 不是 doc 的子级。

有关详细信息,请查看 javadocs。其中有许多重载的 removeContent 方法。

If you want to remove the City element, get its parent and call 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 el is not a child of doc.

Check the javadocs for details. There are a number of overloaded removeContent methods there.

栀子花开つ 2024-11-07 12:06:31

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

相反,这样做:

el.getParentElement().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:

el.getParentElement().detach();

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文