Java XML String to Document xmlNewDocument(final String tagName)

Here you can find the source of xmlNewDocument(final String tagName)

Description

Xml new document.

License

BSD License

Parameter

Parameter Description
tagName the tag name

Return

the element

Declaration

public static final Element xmlNewDocument(final String tagName) 

Method Source Code

//package com.java2s;
/**//from   w  ww .  j a  v a2  s  .  com
 * Copyright (C) 2011-2012 Barchart, Inc. <http://www.barchart.com/>
 *
 * All rights reserved. Licensed under the OSI BSD License.
 *
 * http://www.opensource.org/licenses/bsd-license.php
 */

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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

public class Main {
    private static final ThreadLocal<DocumentBuilder> XML_BUILDER = new ThreadLocal<DocumentBuilder>() {
        @Override
        protected DocumentBuilder initialValue() {
            try {

                final DocumentBuilder builder = DocumentBuilderFactory
                        .newInstance().newDocumentBuilder();

                return builder;

            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    };

    /**
     * Xml new document.
     * 
     * @param tagName
     *            the tag name
     * @return the element
     */
    public static final Element xmlNewDocument(final String tagName) {
        final Document doc = XML_BUILDER.get().newDocument();
        final Element tag = doc.createElement(tagName);
        return tag;
    }
}

Related

  1. toDocument(String xml)
  2. toXmlDocument(final InputStream inputStream, final String path)
  3. toXMLDocument(String xmlString)
  4. writeDocumentToString(Document document)
  5. xml2Document(String xml)
  6. xmlStringToDocument(String xml)
  7. xmlStringToDocument(String xmlInputString)