Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I remove all selected nodes from an XPath?
    primarykey
    data
    text
    <p>I run an XPath in Java with the following xml and code:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;list&gt; &lt;member name="James"&gt; &lt;friendlist&gt; &lt;friend&gt;0001&lt;/friend&gt; &lt;friend&gt;0002&lt;/friend&gt; &lt;friend&gt;0003&lt;/friend&gt; &lt;/friendlist&gt; &lt;/member&gt; &lt;member name="Jamie"&gt; &lt;friendlist&gt; &lt;friend&gt;0003&lt;/friend&gt; &lt;friend&gt;0002&lt;/friend&gt; &lt;friend&gt;0001&lt;/friend&gt; &lt;/friendlist&gt; &lt;/member&gt; &lt;member name="Katie"&gt; &lt;friendlist&gt; &lt;friend&gt;0001&lt;/friend&gt; &lt;friend&gt;0003&lt;/friend&gt; &lt;friend&gt;0004&lt;/friend&gt; &lt;/friendlist&gt; &lt;/member&gt; &lt;/list&gt; </code></pre> <p>Code: </p> <pre><code>try { XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression pathExpr = xpath.compile("/list/member/friendlist/friend[.='0003']"); } catch (XPathExpressionException e) { </code></pre> <p>Of course there are more codes after this but I didn't paste it here because it thought it may confuse even more.</p> <p>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.</p> <p>Thanks!</p> <p>Update: This is how I used my XPath:</p> <pre><code>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(); } </code></pre> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload