XMLUtil { public static void removeNodes(Element parent, NodeList elements) { List<Node> nodesToRemove = new ArrayList<Node>(); for (int i = 0; i < elements.getLength(); i++) { Node item = elements.item(i); if (item.getParentNode() == parent) { nodesToRemove.add(item); } } for (Node n : nodesToRemove) { parent.removeChild(n); } } private  XMLUtil(); static Document parseXML(String xmlAsString); static Document parseXML(InputStream in); static void writeXML(Node xml, File file); static void writeXML(Node xml, OutputStream outputStream); static void removeNodes(Element parent, NodeList elements); static List<Element> getChildElementsByName(Element element,
			String localName); static List<Element> getChildElements(Element element); static void addAsFirstChild(Element element, Element newChild); static QName getQName(Node n); static void removeAllSubNodesExceptAttributes(Node n); static Document createDocument(); static String getContentsOfTextOnlyNode(Node n); static String getXPathForElement(Node e, NamespaceContext ctx); static int getPosition(Element e); static Text appendTextNode(Element e, String contents); static String getTextContent(Node e); static QName resolveQName(String qNameWithPrefix, Element element); static String resolveNamespacePrefix(String prefix, Element element); static QName resolveQNameTargetNS(String sName, Node context, String targetNS);  }
@Test public void testRemoveNodes() throws Exception { Document doc = XMLUtil.parseXML(getClass().getResourceAsStream( "simple.xml")); Element root = doc.getDocumentElement(); List<Element> children = XMLUtil.getChildElements(root); Element a = children.get(0); Element b = children.get(1); Element c = children.get(2); assertEquals(1, root.getElementsByTagName("a").getLength()); assertEquals(1, root.getElementsByTagName("b").getLength()); assertEquals(1, root.getElementsByTagName("c").getLength()); XMLUtil.removeNodes(root, new NodeListMock(a, b)); assertEquals(0, root.getElementsByTagName("a").getLength()); assertEquals(0, root.getElementsByTagName("b").getLength()); assertEquals(1, root.getElementsByTagName("c").getLength()); XMLUtil.removeNodes(root, new NodeListMock(a, c)); assertEquals(0, XMLUtil.getChildElements(root).size()); }