Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;

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

public class Main {
    static DocumentBuilder docBuilder;

    public static Document parseXML(InputSource is) {
        try {
            Document document = docBuilder.parse(is);
            return document;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (SAXException e) {
            throw new RuntimeException(e);
        }
    }

    public static Document parseXML(String input) {
        return parseXML(toInputSource(input));
    }

    public static Document parseXML(InputStream input) {
        return parseXML(toInputSource(input));
    }

    public static Document parseXML(URL input) {
        return parseXML(toInputSource(input));
    }

    public static Document parseXML(File input) {
        return parseXML(toInputSource(input));
    }

    private static InputSource toInputSource(String xml) {
        InputSource iso = new InputSource(new StringReader(xml));
        return iso;
    }

    private static InputSource toInputSource(File xmlFile) {
        InputSource iso;
        try {
            iso = new InputSource(new FileReader(xmlFile));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return iso;
    }

    private static InputSource toInputSource(URL xmlFile) {
        InputSource iso;
        try {
            iso = new InputSource(xmlFile.openStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return iso;
    }

    private static InputSource toInputSource(InputStream is) {
        InputSource iso;
        iso = new InputSource(is);
        return iso;
    }
}