java 如何从 XPath 中删除所有选定的节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6203787/
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 14:49:18  来源:igfitidea点击:

How do I remove all selected nodes from an XPath?

javaxmlxpath

提问by Carven

I run an XPath in Java with the following xml and code:

我使用以下 xml 和代码在 Java 中运行 XPath:

<?xml version="1.0" encoding="UTF-8"?>
<list>
    <member name="James">
        <friendlist>
            <friend>0001</friend>
            <friend>0002</friend>
            <friend>0003</friend>
        </friendlist>
    </member>
    <member name="Jamie">
        <friendlist>
            <friend>0003</friend>
            <friend>0002</friend>
            <friend>0001</friend>
        </friendlist>
    </member>
    <member name="Katie">
        <friendlist>
            <friend>0001</friend>
            <friend>0003</friend>
            <friend>0004</friend>
        </friendlist>
    </member>
</list>

Code:

代码:

try {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression pathExpr = xpath.compile("/list/member/friendlist/friend[.='0003']");
} catch (XPathExpressionException e) {

Of course there are more codes after this but I didn't paste it here because it thought it may confuse even more.

当然在这之后还有更多的代码,但我没有在这里粘贴它,因为它认为它可能会更加混乱。

But the idea is I wish to select all the friend nodes that have the ID 0003 from all the members' friendlist nodes, and then remove it from the XML file. The XPath works by selecting all the "friend" nodes that have the value=0003. I know I can use the removeChild() method of the XML Document object. But the problem is how do I remove all of it directly, without going through layers of loops starting from its parent? The removeChild() method needs me to know its parent's parent's parent.

但我的想法是我希望从所有成员的好友列表节点中选择 ID 为 0003 的所有好友节点,然后将其从 XML 文件中删除。XPath 的工作原理是选择所有值为 0003 的“朋友”节点。我知道我可以使用 XML Document 对象的 removeChild() 方法。但问题是我如何直接删除所有它,而不是从它的父级开始经过层层循环?removeChild() 方法需要我知道其父级的父级。

Thanks!

谢谢!

Update: This is how I used my XPath:

更新:这就是我使用 XPath 的方式:

XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression pathExpr = null;
try {
    pathExpr = xpath.compile("/list/member/friendlist/friend[.='0003']");
} catch (XPathExpressionException e) {
    e.printStackTrace();
}
NodeList list = null;
try {
    list = (NodeList) pathExpr.evaluate(xmlDoc, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
    e.printStackTrace();
}

The xmlDoc is an XML document object that has an XML file parsed. The XML works fine. It is only the XML not returning a reference but a whole new nodelist, which makes it impossible for me to refer back to its original xml document to do amendments.

xmlDoc 是一个 XML 文档对象,它解析了一个 XML 文件。XML 工作正常。只有XML没有返回引用,而是一个全新的节点列表,这让我无法参考其原始xml文档进行修改。

回答by jtahlborn

for each node in the returned NodeList:

对于返回的 NodeList 中的每个节点:

n.getParentNode().removeChild(n);

回答by LarsH

I don't understand why the returned nodelist's nodes are returning null for parentNode().

我不明白为什么返回的节点列表的节点为 parentNode() 返回 null。

But you could try first selecting all the parents of the nodes you want to remove, with this XPath expression:

但是您可以先尝试使用以下 XPath 表达式选择要删除的节点的所有父节点:

"/list/member/friendlist[friend[.='0003']]"

or the equivalent,

或等价物,

"/list/member/friendlist[friend = '0003']]"

Then iterate through the resulting nodelist, and in the context of each one, query for nodes matching the XPath expression

然后遍历生成的nodelist,在每一个的上下文中,查询匹配XPath表达式的节点

"friend[.='0003']"

That will give you a parent node and a child node to use with removeChild().

这将为您提供一个父节点和一个子节点以用于removeChild().

回答by BaSche

Have a look on XUpdate. It's not pretty, but it works.

看看XUpdate。它不漂亮,但它有效。