Java XML Document Create createEmptyElement(Document doc, QName qname)

Here you can find the source of createEmptyElement(Document doc, QName qname)

Description

create Empty Element

License

Apache License

Parameter

Parameter Description
doc The Document that is the factory for the new Element.
qname The QName of the new Element.

Return

An empty Element whose owner is the given Document.

Declaration

private static Element createEmptyElement(Document doc, QName qname) 

Method Source Code

//package com.java2s;
/* /*  w w  w .j  av  a 2s .  c  o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

import javax.xml.namespace.QName;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
    /**
     *
     * @param doc
     *        The Document that is the factory for the new Element.
     * 
     * @param qname
     *        The QName of the new Element.
     * 
     * @return An empty Element whose owner is the given Document.
     *
     */
    private static Element createEmptyElement(Document doc, QName qname) {
        String prefix = qname.getPrefix();
        String name = qname.getLocalPart();

        //
        // NOTE: The DOM API requires that elements with qualified names 
        //       be created with prefix:localName as the tag name. We 
        //       CANNOT just use the localName and expect the API to fill 
        //       in a prefix, nor can we use setPrefix after creation. For 
        //       parsing to work correctly, the second argument to the 
        //       createElementNS method MUST be a qualified name.
        //
        if (prefix != null && prefix.length() > 0)
            name = prefix + ':' + name;

        String uri = qname.getNamespaceURI();

        return doc.createElementNS(uri, name);
    }
}

Related

  1. createEmptyDocument()
  2. createEmptyDocument()
  3. createEmptyDocument()
  4. createEmptyDocument()
  5. createEmptyDocument(final DocumentBuilder builder)
  6. createNewDocument()
  7. createNewDocument(DocumentBuilder docBuilder)
  8. createNewDocument(String namespaceURI, String qualifiedName)
  9. getDocument()