In the below sample XML, how to remove Entire B Node if E=13 using java parser.
<xml>
<A>
<B>
<C>
<E>11</E>
<F>12</F>
</C>
</B>
<B>
<C>
<E>13</E>
<F>14</F>
</C>
</B>
</A>
Please advise.
Alternative DOM Approach
Alternatively, instead of doing a brute force traversal of the XML document you could use the XPath capabilities in the JDK to find the “B” element with value “13” and then remove it from its parent:
The advantage of using an XPath it’s easier to maintain, if the structure changes it’s just a one line change to your code. Also if the depth of your document grows the XPath based solution stays the same number of lines.
Non-DOM Approach
If you don’t want to materialize your XML as a DOM. You could use a Transformer and a stylesheet to remove a node: