Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.InputStream;
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;

public class Main {
    public static Document toXML(InputSource input) {
        try {
            return getDocBuilder().parse(input);
        } catch (Exception e) {
            throw new RuntimeException("Error while creating xml doc", e);
        }
    }

    public static Document toXML(InputStream input) {
        return toXML(new InputSource(input));
    }

    public static Document toXML(String input) {
        return toXML(new InputSource(new StringReader(input)));
    }

    private static DocumentBuilder getDocBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        return docBuilder;
    }
}