Java Utililty Methods XML Element Namespace

List of utility methods to do XML Element Namespace

Description

The list of methods to do XML Element Namespace are organized into topic(s).

Method

voidaddNamespaceDeclaration(Element element, String namespacePrefix, String namespaceURI)
Adds a namespace declaration attribute to the given element.
element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
        XMLConstants.XMLNS_ATTRIBUTE + ':' + namespacePrefix, namespaceURI);
voidaddNamespacePrefix(Element element, String namespaceUri, String prefix)
Add a namespace prefix definition to an element.
element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix, namespaceUri);
MapbuildNamespacePrefixMap(final Element root)
Build Map of namespace URIs to prefixes.
final HashMap<String, String> namespacePrefixMap = new HashMap<>();
NamedNodeMap attributes = root.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
    Node item = attributes.item(i);
    if (item.getNodeName().startsWith("xmlns")) {
        String[] splitName = item.getNodeName().split(":");
        String prefix;
        if (splitName.length > 1) {
...
MapbuildNamespacePrefixMap(final Element root)
Build Map of namespace URIs to prefixes.
final HashMap<String, String> namespacePrefixMap = new HashMap<>();
NamedNodeMap attributes = root.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
    Node item = attributes.item(i);
    if (item.getNodeName().startsWith("xmlns")) {
        String[] splitName = item.getNodeName().split(":");
        String prefix;
        if (splitName.length > 1) {
...
ElementbyteArrayToElement(byte[] b, boolean namespaceAware)
Convert the byte array representation of an Element into an Element
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(namespaceAware);
docBuilderFactory.setValidating(false);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new ByteArrayInputStream(b));
return doc.getDocumentElement();
ElementcreateElement(final String namespace, final String localPart, final String value)
create Element
if (document == null) {
    throw new RuntimeException("XMLDocumentBuilder could not be initialized");
Element element = document.createElementNS(namespace, "ns:" + localPart);
element.appendChild(document.createTextNode(value));
return element;
ElementcreateElement(String namespaceURI, String name)
create Element
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
try {
    final DocumentBuilder builder = builderFactory.newDocumentBuilder();
    final Document document = builder.newDocument();
    return document.createElementNS(namespaceURI, name);
} catch (ParserConfigurationException e) {
    throw new RuntimeException("Couldn't get a DocumentBuilder", e);
...
ElementcreateElement(String namespaceURI, String name)
create Element
return getDocument().createElementNS(namespaceURI, name);
StringcreateNamespace(Element el, String ns)
create Namespace
String p = "ns1";
int i = 1;
while (getPrefix(el, ns) != null) {
    p = "ns" + i;
    i++;
addNamespacePrefix(el, ns, p);
return p;
...
ListgetAllNamespaceDeclarations(Element element)
get All Namespace Declarations
List<QName> nodes = new ArrayList<>();
NamedNodeMap map = element.getAttributes();
for (int i = 0; i < map.getLength(); i++) {
    Node node = map.item(i);
    String nsUri = node.getNamespaceURI();
    if (nsUri == null) {
        continue;
    if ("http://www.w3.org/2000/xmlns/".equals(nsUri)) {
        nodes.add(new QName(node.getTextContent(), node.getLocalName(), ""));
return nodes;