Java XML Document from File readXMLFile(String filename)

Here you can find the source of readXMLFile(String filename)

Description

Reads an XML document and returns the root node (which is all you need 99% of the time)

License

Open Source License

Declaration

public static Node readXMLFile(String filename) throws Exception 

Method Source Code


//package com.java2s;
import org.w3c.dom.Node;

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

import org.xml.sax.SAXParseException;
import org.w3c.dom.Document;
import java.io.File;

public class Main {
    /**//from  ww w.j a  v a  2s. c  om
       Reads an XML document and returns the root node
       (which is all you need 99% of the time)
    */
    public static Node readXMLFile(String filename) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = null;

        // simplify exceptions because we are only interested in parsing
        // errors.       
        try {
            document = builder.parse(new File(filename));
        } catch (SAXParseException spe) {

            String errtxt = "parse error in " + spe.getSystemId() + ",\nline: " + spe.getLineNumber() + " "
                    + spe.getMessage();

            throw new IllegalArgumentException(errtxt);
        }

        return document.getDocumentElement();
    }
}

Related

  1. readXML(File xml)
  2. readXml(String path)
  3. readXML(String path)
  4. readXMLFile(File file)
  5. readXmlFile(String fileName)
  6. readXmlFile(String fileName)
  7. readXMLFile(String filename)
  8. ReadXMLFile1(String in_FileName, String in_TagName)
  9. ReadXMLFile3(String in_FileName, String in_TagName)