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.InputStream;

import java.io.Reader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class Main {
    /**
     * Document builder factory
     */
    private static DocumentBuilderFactory factory = null;

    /**
     * Creates a DOM from a file representation of an xml record
     * 
     * @param doc
     *            the xml file
     * @return the DOM document
     */
    public static Document parseDomFromFile(File doc) {
        FileReader reader;
        try {
            reader = new FileReader(doc);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Could not open file '" + doc.getName() + "'!", e);
        }
        return parseDom(reader);
    }

    /**
     * Creates a DOM from a file representation of an xml record
     * 
     * @param reader
     *            the xml reader
     * @return the DOM document
     */
    public static Document parseDom(Reader reader) {
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse(new org.xml.sax.InputSource(reader));
        } catch (Exception e) {
            throw new RuntimeException("Could not parse DOM for '" + reader.toString() + "'!", e);
        }
    }

    public static Document parseDom(InputStream byteArrayInputStream) {

        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse(new org.xml.sax.InputSource(byteArrayInputStream));
        } catch (Exception e) {
            throw new RuntimeException("Could not parse DOM for '" + byteArrayInputStream.toString() + "'!", e);
        }

    }
}