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.net.URL;

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

import org.w3c.dom.Document;

public class Main {
    private static DocumentBuilderFactory mFactory = null;

    /**
     * @param xmlURL is a URL to an XML document
     * @return the Document contained in the content at tha tURL
     */
    public static Document readURL(String xmlURL) {
        ensureFactory();
        try {
            URL u = new URL(xmlURL);
            DocumentBuilder builder = mFactory.newDocumentBuilder();
            return builder.parse(u.openStream());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static synchronized void ensureFactory() {
        if (mFactory == null) {
            mFactory = DocumentBuilderFactory.newInstance();
            mFactory.setValidating(false);
            mFactory.setCoalescing(true);
            mFactory.setExpandEntityReferences(false);
        }
    }
}