Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2010-2012 Research In Motion Limited. All rights reserved.
 *
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License, Version 1.0,
 * which accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main {
    /**
     * open instream for a XML file from URL
     *
     * @param the
     *            source url
     * @return the DOM document
     */
    public static Document openXMLStream(URL url) {
        try {
            // Create a builder factory
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            InputStream stream = url.openStream();

            // Create the builder and parse the stream
            Document doc = factory.newDocumentBuilder().parse(stream);

            return doc;
        } catch (SAXException e) {
            // A parsing error occurred; the xml input is not valid
        } catch (ParserConfigurationException ex) {
            System.out.println(ex.getMessage());
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }

        return null;
    }
}