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

时间:2022-09-24 23:33:09

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.

但我的想法是希望从所有成员的friendlist节点中选择具有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文档进行修改。

3 个解决方案

#1


9  

for each node in the returned NodeList:

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

n.getParentNode().removeChild(n);

#2


3  

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

我不明白为什么返回的nodelist节点为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

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

"friend[.='0003']"

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

这将为您提供一个父节点和一个与removeChild()一起使用的子节点。

#3


1  

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

看看XUpdate。它不漂亮,但它的工作原理。

#1


9  

for each node in the returned NodeList:

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

n.getParentNode().removeChild(n);

#2


3  

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

我不明白为什么返回的nodelist节点为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

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

"friend[.='0003']"

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

这将为您提供一个父节点和一个与removeChild()一起使用的子节点。

#3


1  

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

看看XUpdate。它不漂亮,但它的工作原理。