Android URL Read readXmlFromUrl(String address)

Here you can find the source of readXmlFromUrl(String address)

Description

read Xml From Url

Declaration

public static Document readXmlFromUrl(String address) 

Method Source Code

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
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 {
    public static Document readXmlFromUrl(String address) {

        Document document = null;
        HttpURLConnection urlConnection = null;
        try {/*from   w w w  . j av  a 2s. c o  m*/
            URL url = new URL(address);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = new BufferedInputStream(
                    urlConnection.getInputStream());

            // Create XML document from connection
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(inputStream);

        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Malformed address "
                    + address);
        } catch (IOException e) {
            throw new IllegalArgumentException("Unable to open adress");
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }

        return document;
    }
}

Related

  1. getResource(String filename)
  2. readFile(URL url)
  3. getXML(String url)
  4. getStringFromUrl(String url)
  5. getFileDateTime(URL url)
  6. readTextFromUri(Context c, Uri uri)
  7. getHtmlByteArray(final String url)