Java XML Element Namespace addNamespaceDeclaration(Element element, String namespacePrefix, String namespaceURI)

Here you can find the source of addNamespaceDeclaration(Element element, String namespacePrefix, String namespaceURI)

Description

Adds a namespace declaration attribute to the given element.

License

Apache License

Declaration

public static void addNamespaceDeclaration(Element element, String namespacePrefix, String namespaceURI) 

Method Source Code

//package com.java2s;
/* NOTICE: This file has been changed by Plutext Pty Ltd for use in docx4j.
 * The package name has been changed; there may also be other changes.
 * //from  w w  w.j  a v  a  2  s.  c o m
 * This notice is included to meet the condition in clause 4(b) of the License. 
 */

import javax.xml.XMLConstants;

import javax.xml.stream.events.Namespace;

import org.w3c.dom.Element;

public class Main {
    /**
     * Adds a namespace declaration attribute to the given element.
     */
    public static void addNamespaceDeclaration(Element element, String namespacePrefix, String namespaceURI) {
        element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                XMLConstants.XMLNS_ATTRIBUTE + ':' + namespacePrefix, namespaceURI);
    }

    /**
     * Adds a namespace declaration attribute to the given element.
     */
    public static void addNamespaceDeclaration(Element element, Namespace namespace) {
        addNamespaceDeclaration(element, namespace.getPrefix(), namespace.getNamespaceURI());
    }
}

Related

  1. addNamespacePrefix(Element element, String namespaceUri, String prefix)
  2. buildNamespacePrefixMap(final Element root)
  3. buildNamespacePrefixMap(final Element root)
  4. byteArrayToElement(byte[] b, boolean namespaceAware)