Reputation: 5850
I have this example xml:
<Forms>
<Form Desc="sasld" DocType="1">
<topmostSubform ind="0">
<HouseNo ind="0">413</HouseNo>
<ZipCode ind="0">82051</ZipCode>
<PostOfficeBox ind="0">0</PostOfficeBox>
</topmostSubform>
</Form>
<Form Desc="abcd" DocType="1">
<topmostSubform ind="0">
<TextField1 ind="24" />
<TextField1 ind="25" />
<TextField1 ind="26" />
<DisContActivity-5-0 ind="0" />
<DisWithFranActivity-5-0 ind="0" />
</topmostSubform>
</Form>
<Form Desc="abcd" DocType="5">
<topmostSubform ind="0">
<TextField1 ind="24" />
<TextField1 ind="25" />
</topmostSubform>
</Form>
I want to remove all nodes which are of type Form and its DocType attribute value is 1.
I hold this xml in a Document object.
i tried:
String xpath_string = "//Form[@DocType ='1']";
XPathExpression xPathExpr = XPATH.compile(xpath_string);
Object result_obj = xPathExpr.evaluate(document,XPathConstants.NODESET);
NodeList nodes = (NodeList) result_obj;
System.out.println(nodes.getLength());
for(int i=1;i<nodes.getLength();i++)
document.removeChild(nodes.item(i));
But it gives "NOT_FOUND_ERR".
Upvotes: 0
Views: 1086
Reputation: 122414
document.removeChild(nodes.item(i));
attempts to remove a child node from the document node. But the node you're trying to remove isn't a child of the document node, it's a child of the Forms
element. Try this instead:
nodes.item(i).getParentNode().removeChild(nodes.item(i);
Your for
loop also needs to start from 0, not 1, as DOM node list indexes are 0-based.
Upvotes: 2