Java XML Document from File read(String filename)

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

Description

read

License

Open Source License

Declaration

public static Document read(String filename) 

Method Source Code


//package com.java2s;

import java.io.File;

import java.io.IOException;

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    public static Document read(String filename) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);//from   w  w  w  . ja va  2  s  .c  o m
        DocumentBuilder builder = null;
        Document document = null;
        try {
            builder = factory.newDocumentBuilder();
            builder.setEntityResolver(new EntityResolver() {
                public InputSource resolveEntity(String publicId, String systemId)
                        throws SAXException, IOException {
                    return new InputSource(new StringReader(""));
                }
            });
            document = builder.parse(new File(filename));
            document.getDocumentElement().normalize();
        } catch (Exception e) {
        }
        return document;
    }
}

Related

  1. loadXmlFromFile(String xmlFileName)
  2. read(InputStream input)
  3. read(InputStream xml)
  4. read(java.io.File file)
  5. read(String file)
  6. readDocument(String fileName)
  7. readDocumentXML(File file)
  8. readXml(File file)
  9. readXML(File file)