Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;

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

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main {
    /**
     * Parse a XML file and return the resulting DOM tree.
     */
    public static Document readXml(URL xmlUrl) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = newNamespaceAwareFactory();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputStream input = xmlUrl.openStream();
        try {
            return builder.parse(input);
        } finally {
            input.close();
        }
    }

    public static DocumentBuilderFactory newNamespaceAwareFactory() {
        /*
         * TODO return DocumentBuilderFactory.newInstance(
         * "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl",
         * XmlHelper.class.getClassLoader());
         */
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        /*
         * Somewhat required for proper JAXB usage... Really? Why I've wrote
         * that? Default value is false and it seems to works like a charm
         * whatever the value.
         */
        factory.setNamespaceAware(true);
        return factory;
    }
}