Java XML Element Create createElement(QName qName)

Here you can find the source of createElement(QName qName)

Description

Creates a new Element having the specified qualified name.

License

Apache License

Parameter

Parameter Description
qName A QName object.

Return

An Element node (with a Document owner but no parent).

Declaration

public static Element createElement(QName qName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

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

public class Main {
    /**/*from   www  . j a  v a 2  s. c  om*/
     * Creates a new Element having the specified qualified name. The element
     * must be {@link Document#adoptNode(Node) adopted} when inserted into
     * another Document.
     *
     * @param qName A QName object.
     * @return An Element node (with a Document owner but no parent).
     */
    public static Element createElement(QName qName) {
        Document doc = null;
        try {
            doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e);
        }
        Element elem = doc.createElementNS(qName.getNamespaceURI(), qName.getLocalPart());
        return elem;
    }
}

Related

  1. createElement(final Document parentDocument, final String name)
  2. createElement(Node element, String name)
  3. createElement(Node node, String name)
  4. createElement(Node parent, String tag)
  5. createElement(QName key)
  6. createElement(String name, Object value, Document doc)
  7. createElement(String tag, String textContent, Document xml)
  8. createElement(String tagName, String text, Document doc)
  9. createElementAndText(Element parent, String elementName, String text)