Java XML Document from URL getXMLFromURL(String url)

Here you can find the source of getXMLFromURL(String url)

Description

get XML From URL

License

LGPL

Declaration

public static Document getXMLFromURL(String url) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

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

public class Main {
    static DocumentBuilderFactory builderFactory = DocumentBuilderFactory
            .newInstance();//from  w  ww  . j av a 2  s .c o  m
    static DocumentBuilder docBuilder;

    public static Document getXMLFromURL(String url) {
        Document xmlDoc = null;

        try {
            docBuilder = builderFactory.newDocumentBuilder();
            xmlDoc = docBuilder.parse(url);
        } catch (ParserConfigurationException error) {
            System.err.println("Error reading parser configuration.");
            return xmlDoc;
        } catch (SAXException error) {
            System.err.println("Error parsing document.");
            return xmlDoc;
        } catch (IOException error) {
            System.err.println("Error accessing URL.");
            return xmlDoc;
        }

        xmlDoc.getDocumentElement().normalize();
        return xmlDoc;
    }
}

Related

  1. getXMLFile(URL url)