read xml File - Android XML

Android examples for XML:XML Parse

Description

read xml File

Demo Code


//package com.java2s;
import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {

    public static Element readxml(String filePath) {
        Document document = null;
        try {//from w ww.  j a v a 2 s.co m
            DocumentBuilderFactory dbf = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            document = db.parse(filePath);
        } catch (Exception e) {
            throw new RuntimeException(
                    "<<SmartXmlUtil>> cannnot parse xml file : " + filePath,
                    e);

        }
        return document.getDocumentElement();
    }

    public static Element readxml(File xmlFile) {
        Document document = null;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            document = db.parse(xmlFile);
        } catch (Exception e) {
            throw new RuntimeException(
                    "<<SmartXmlUtil>> cannnot parse xml file : "
                            + xmlFile.getAbsolutePath(), e);

        }
        return document.getDocumentElement();
    }
}

Related Tutorials