Java examples for XML:DOM Document
register Namespace for XML Document
//package com.java2s; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; public class Main { public static void registerNamespace(Document d, String targetNamespace) { if (targetNamespace == null || targetNamespace.length() == 0) return; Element e = d.getDocumentElement(); int i = 0; NamedNodeMap map = e.getAttributes(); String prefix = "ns" + String.valueOf(map.getLength()); int n = map.getLength(); for (i = 0; i < n; i++) { /*Node node = map.item(i); if(node.getNodeType()!=Node.ATTRIBUTE_NODE) continue;*///from w ww . jav a 2 s . co m Attr attr = (Attr) map.item(i); String nsUri = attr.getNamespaceURI(); if (nsUri == null) continue; if (nsUri.equals("http://www.w3.org/2000/xmlns/")) { if (targetNamespace.equals(attr.getValue())) return; } } Attr atr = d.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix); atr.setValue(targetNamespace); e.setAttributeNode(atr); } }