Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
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 getDocument() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        Document doc = null;
        try {
            builder = factory.newDocumentBuilder();
            doc = builder.newDocument();
        } catch (ParserConfigurationException e) {
            System.out.println("Error : XmlUtil.getDocument");
            e.printStackTrace();
        } finally {
        }
        return doc;
    }

    public static Document getDocument(File file) throws IOException {
        Document doc = null;

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        ;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = null;

            builder = factory.newDocumentBuilder();

            InputSource inputSource = new InputSource(br);
            doc = builder.parse(inputSource);
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            br.close();
            br = null;
        }
        return doc;
    }

    public static Document getDocument(String str) {
        Document doc = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = null;

            builder = factory.newDocumentBuilder();

            StringReader userdataReader = new StringReader(str);

            InputSource inputSource = new InputSource(userdataReader);

            doc = builder.parse(inputSource);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return doc;
    }
}