I am creating a Dom (org.w3c.dom.Document) document from a XML File. I want to remove all the namespace from that document to invoke some other service. That service expecting an XML without name space.
public Document cleanNameSpace(Document doc) {
NodeList list = doc.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
removeNamSpace(list.item(i), "");
}
return doc;
}
private void removeNamSpace(Node node, String nameSpaceURI) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Document ownerDoc = node.getOwnerDocument();
NamedNodeMap map = node.getAttributes();
Node n;
while (!(0==map.getLength())) {
n = map.item(0);
map.removeNamedItemNS(n.getNamespaceURI(), n.getLocalName());
}
ownerDoc.renameNode(node, nameSpaceURI, node.getLocalName());
}
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
removeNamSpace(list.item(i), nameSpaceURI);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With