read XML from uri XML - Android XML

Android examples for XML:XML Parse

Description

read XML from uri XML

Demo Code

/********************************************************************************
 *
 *   Copyright (C) 2005  Svyatoslav Urbanovych
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *********************************************************************************/
//package com.java2s;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;

public class Main {
    public static void main(String[] argv) throws Exception {
        String uriXML = "java2s.com";
        System.out.println(readXML(uriXML));
    }//from  ww w  .  j  ava2s  . c  om

    public static Document readXML(String uriXML, boolean valid)
            throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(valid);
        if (uriXML.toUpperCase().trim().indexOf("HTTP:") == -1) {
            return dbf.newDocumentBuilder().parse(new File(uriXML));
        } else
            return DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder().parse(uriXML);
    }

    public static Document readXML(String uriXML) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        if (uriXML.toUpperCase().trim().indexOf("HTTP:") == -1) {
            return dbf.newDocumentBuilder().parse(new File(uriXML));
        } else
            return DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder().parse(uriXML);
    }
}

Related Tutorials