load XML Document from file path - Android XML

Android examples for XML:XML Document

Description

load XML Document from file path

Demo Code


//package com.java2s;

import java.io.File;

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

import org.w3c.dom.Document;

public class Main {
    public static void main(String[] argv) throws Exception {
        String path = "java2s.com";
        System.out.println(loadXMLDocument(path));
    }/*from   w  ww . j av a2s . co  m*/

    public static Document loadXMLDocument(String path) {
        return loadXMLDocument(new File(path));
    }

    public static Document loadXMLDocument(File file) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(file);
            return document;
        } catch (Exception ex) {
            return null;
        }
    }
}

Related Tutorials