Java XML Element Create createElementNS(Document doc, String namespaceURI, String prefix, String nodeName)

Here you can find the source of createElementNS(Document doc, String namespaceURI, String prefix, String nodeName)

Description

Creates and returns a new XML element node with the given name and namespace URI.

License

Open Source License

Parameter

Parameter Description
doc needed for creating the XML node
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 ':'

Return

a new XML node

Declaration

public static Element createElementNS(Document doc, String namespaceURI, String prefix, String nodeName) 

Method Source Code

//package com.java2s;
/*/*from w w w .j a  v  a2  s  .c  o  m*/
 * 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.Document;

import org.w3c.dom.Element;

public class Main {
    private static boolean printXMLNamespaces;

    /**
     * Creates and returns a new XML element node with the given name and namespace URI.
     * The full element's 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 doc needed for creating the XML node
     * @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 ':'
     * @return a new XML node
     * @see Document#createElementNS(java.lang.String, java.lang.String)
     */
    public static Element createElementNS(Document doc, String namespaceURI, String prefix, String nodeName) {
        String fullName = (prefix == null) ? nodeName : prefix + ":" + nodeName;
        Element e = doc.createElementNS(namespaceURI, fullName);
        if (printXMLNamespaces) {
            String attrName = (prefix == null) ? "xmlns" : "xmlns:" + prefix;
            e.setAttribute(attrName, namespaceURI);
        }
        return e;
    }
}

Related

  1. createElementInSameNamespace(Element parent, String localName)
  2. createElementInTempDocument(String name, String prefix, String namespaceURI)
  3. createElementLn(Document d, String name, String value, boolean isCDATA)
  4. createElementMapping(final Document doc1, final Document doc2)
  5. createElementMappingForNodes(final Node n1, final Node n2, final Map mapping)
  6. createElementNS(Document root, String namespaceURI, String qualifiedName)
  7. createElementNS(String namespaceURI, String qualifiedName, String text, Document doc)
  8. createElementNsIn(Element parent, String ns, String name, String textContent)
  9. createElementWithText(Element parent, String name, String value)