Java XML Document from URL getXMLFile(URL url)

Here you can find the source of getXMLFile(URL url)

Description

Gets an XML file from an url

License

Open Source License

Parameter

Parameter Description
url - url

Exception

Parameter Description
ParserConfigurationException - thrown if the document can not be parsed as an XML
IOException- thrown if the URL can not be opened
SAXException an exception

Return

Document that is an XML file

Declaration

public static Document getXMLFile(URL url)
        throws ParserConfigurationException, IOException, SAXException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.net.URL;

public class Main {
    public static Document getXMLFile(String url)
            throws ParserConfigurationException, IOException, SAXException {
        return getXMLFile(new URL(url));
    }//w  w w  .ja  va2s  . c om

    /**
     * Gets an XML file from an url
     *
     * @param url - url
     * @return Document that is an XML file
     * @throws ParserConfigurationException - thrown if the document can not be parsed as an XML
     * @throws IOException                  - thrown if the URL can not be opened
     * @throws SAXException
     */
    public static Document getXMLFile(URL url)
            throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        return db.parse(url.openStream());
    }
}

Related

  1. getXMLFromURL(String url)