parse XML File using DOM Parser - Android XML

Android examples for XML:DOM

Description

parse XML File using DOM Parser

Demo Code


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

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

import org.w3c.dom.Document;

public class Main {
    public static void main(String[] argv) throws Exception {
        File file = new File("Main.java");
        System.out.println(parseFile(file));
    }//from   w w w.j  av a2s .c om

    public static Document parseFile(File file)
            throws org.xml.sax.SAXException, IOException,
            ParserConfigurationException {
        return newDocumentBuilder().parse(file);
    }

    private static DocumentBuilder newDocumentBuilder()
            throws ParserConfigurationException {
        // SAXParsers are not concurrency compatible, so always return a new instance to prevent thread issues 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        return dbf.newDocumentBuilder();
    }
}

Related Tutorials