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 org.w3c.dom.Document;

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

public class Main {
    private static DocumentBuilder documentBuilder;

    public static Document parseXmlStringToDocument(String xmlString) {

        if (xmlString == null)
            return null;

        Document document;

        try {

            InputSource source = new InputSource(new StringReader(xmlString));

            document = documentBuilder.parse(source);
        } catch (IOException ex) {

            throw new RuntimeException("Failed to parse input stream due to I/O errors: " + ex.getMessage(), ex);
        } catch (SAXException ex) {

            throw new RuntimeException("Failed to parse input stream due to SAX errors: " + ex.getMessage(), ex);
        }

        return document;
    }
}