Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Removes all nodes of specified namespace * * @param dom w3c dom to be cleaned from spec. ns nodes * @param namespace */ public static void removeAllNodesOfNS(Document dom, String namespace) { List<Node> nodes = getAllNodesByNamespaceRecursive(dom, namespace); for (Node node : nodes) { node.getParentNode().removeChild(node); } } /** * Recursively fetches all nodes of specified ns. For multi ns documents * * @param node the starting node. * @param namespace desired ns */ private static List<Node> getAllNodesByNamespaceRecursive(Node node, String namespace) { List nsNodeList = new ArrayList(); if (node.getNamespaceURI() != null && node.getNamespaceURI().equals(namespace)) { nsNodeList.add(node); } NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); ++i) { nsNodeList.addAll(getAllNodesByNamespaceRecursive(list.item(i), namespace)); } return nsNodeList; } }