Java XML Document Create stringToDocument(String string)

Here you can find the source of stringToDocument(String string)

Description

string To Document

License

EUPL

Declaration

public static Document stringToDocument(String string) throws SAXException, IOException 

Method Source Code


//package com.java2s;
/*//from w w  w. j  a va 2s  . c  o  m
 * **************************************************-
 * ingrid-interface-csw
 * ==================================================
 * Copyright (C) 2014 - 2016 wemove digital solutions GmbH
 * ==================================================
 * Licensed under the EUPL, Version 1.1 or ? as soon they will be
 * approved by the European Commission - subsequent versions of the
 * EUPL (the "Licence");
 * 
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * 
 * http://ec.europa.eu/idabc/eupl5
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * **************************************************#
 */

import java.io.IOException;
import java.io.StringReader;

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

import org.w3c.dom.Document;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    private static ThreadLocal<DocumentBuilder> threadLocalDocumentBuilder = new ThreadLocal<DocumentBuilder>() {
        @Override
        public DocumentBuilder initialValue() {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);
            DocumentBuilder builder;
            try {
                builder = domFactory.newDocumentBuilder();
                return builder;
            } catch (ParserConfigurationException e) {
                new RuntimeException("Error creating DocumentBuilder", e);
            }
            return null;
        }
    };

    public static Document stringToDocument(String string) throws SAXException, IOException {
        DocumentBuilder builder = threadLocalDocumentBuilder.get();
        builder.reset();
        Document doc = builder.parse(new InputSource(new StringReader(string)));
        return doc;
    }
}

Related

  1. string2Document(String xml, String encode)
  2. stringToDocument(final String xml)
  3. stringToDocument(String doc)
  4. stringToDocument(String string)
  5. stringToDocument(String string)
  6. stringToDocument(String xml)
  7. stringToDocument(String xmlStr, boolean isValidating)
  8. stringToDocument(String xmlString)
  9. toDocument(File f)