Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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 {
    /**
     * 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);
        }
        StringBuilder nodeName = new StringBuilder();
        if (qName.getPrefix().isEmpty()) {
            nodeName.append(qName.getLocalPart());
        } else {
            nodeName.append(qName.getPrefix()).append(':').append(qName.getLocalPart());
        }
        Element elem = doc.createElementNS(qName.getNamespaceURI(), nodeName.toString());
        return elem;
    }
}