Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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 final ThreadLocal<DocumentBuilderFactory> documentBuilderFactory = new ThreadLocal<DocumentBuilderFactory>() {
        protected DocumentBuilderFactory initialValue() {
            return DocumentBuilderFactory.newInstance();
        };
    };

    /**
     * Read an input stream in as an XML document.
     * @param xmlString
     * @return the XML document
     */
    public static Document readXmlDocumentFromString(String xmlString) {
        try {
            DocumentBuilder documentBuilder = documentBuilderFactory.get().newDocumentBuilder();
            return documentBuilder.parse(new InputSource(new StringReader(xmlString)));
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e);
        } catch (SAXException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}