Java XML Element Create createElement(QName key)

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

Description

create Element

License

Apache License

Parameter

Parameter Description
key Fully qualified element name.

Declaration

public static Element createElement(QName key) 

Method Source Code

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

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

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

public class Main {
    /**//from  w w  w. j a  va2 s  . co m
     * @param key Fully qualified element name.
     */
    public static Element createElement(QName key) {
        return createDocument().createElementNS(key.getNamespaceURI(),
                key.getPrefix() + ":" + key.getLocalPart());
    }

    /**
     * @return New XML document from the default document builder factory.
     */
    private static Document createDocument() {
        DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return builder.newDocument();
    }
}

Related

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