Java XML Attribute Set setAttributeNS(Element node, String namespaceURI, String prefix, String nodeName, String value)

Here you can find the source of setAttributeNS(Element node, String namespaceURI, String prefix, String nodeName, String value)

Description

Adds the named attribute with the given namespace URI and value to the given element node.

License

Open Source License

Parameter

Parameter Description
node the node the attribute will be added to
namespaceURI the URI of the underlying namespace
prefix the namespace prefix for this attribute (w/o ':'); may be null
nodeName the node name w/o prefix and ':'
value the attribute's value

Declaration

public static void setAttributeNS(Element node, String namespaceURI, String prefix, String nodeName,
        String value) 

Method Source Code

//package com.java2s;
/*//from w  ww  . ja  va  2  s. com
 * The MIT License (MIT)
 * 
 * Copyright (c) 2010 Technische Universitaet Berlin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import org.w3c.dom.Element;

public class Main {
    private static boolean printXMLNamespaces;

    /**
     * Adds the named attribute with the given namespace URI and value to the given element node.
     * The full attribute name is constructed by the prefix followed by a colon and finally the real node name.
     * If the prefix is null, the full name equals the node name.
     * This method contains a workaround for older Java versions.
     * 
     * @param node the node the attribute will be added to
     * @param namespaceURI the URI of the underlying namespace
     * @param prefix the namespace prefix for this attribute (w/o ':'); may be null
     * @param nodeName the node name w/o prefix and ':'
     * @param value the attribute's value
     * @see Element#setAttributeNS(java.lang.String, java.lang.String, java.lang.String)
     */
    public static void setAttributeNS(Element node, String namespaceURI, String prefix, String nodeName,
            String value) {
        String fullName = (prefix == null) ? nodeName : prefix + ":" + nodeName;
        node.setAttributeNS(namespaceURI, fullName, value);
        if (printXMLNamespaces) {
            String attrName = (prefix == null) ? "xmlns" : "xmlns:" + prefix;
            node.setAttribute(attrName, namespaceURI);
        }
    }
}

Related

  1. setAttribute(Node node, String attName, String val)
  2. setAttribute(Node node, String attr, String value)
  3. setAttribute(Node node, String key, String value)
  4. setAttribute(Node pNode, String attrName)
  5. setAttribute(String name, String value, Element el)
  6. setAttributeValue(Element element, String attribute, String value)
  7. setAttributeValue(final Element target, final String attributeName, final String value)
  8. setAttributeValue(Node node, String attName, String attValue)
  9. setAttributeValue(Node node, String attribute, String value)