Java XML DOM Parse parseDomFromFile(File doc)

Here you can find the source of parseDomFromFile(File doc)

Description

Creates a DOM from a file representation of an xml record

License

Apache License

Parameter

Parameter Description
doc the xml file

Return

the DOM document

Declaration

public static Document parseDomFromFile(File doc) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.InputStream;

import java.io.Reader;

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

import org.w3c.dom.Document;

public class Main {
    /**/*from   ww  w .  ja  v a 2  s.  c  o m*/
     * Document builder factory
     */
    private static DocumentBuilderFactory factory = null;

    /**
     * Creates a DOM from a file representation of an xml record
     * 
     * @param doc
     *            the xml file
     * @return the DOM document
     */
    public static Document parseDomFromFile(File doc) {
        FileInputStream reader;
        try {
            reader = new FileInputStream(doc);
            //            reader = new FileReader(doc);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Could not open file '" + doc.getName() + "'!", e);
        }
        return parseDom(reader);
    }

    /**
     * Creates a DOM from a file representation of an xml record
     * 
     * @param reader
     *            the xml reader
     * @return the DOM document
     */
    public static Document parseDom(Reader reader) {
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse(new org.xml.sax.InputSource(reader));
        } catch (Exception e) {
            throw new RuntimeException("Could not parse DOM for '" + reader.toString() + "'!", e);
        }
    }

    public static Document parseDom(InputStream reader) {
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse(new org.xml.sax.InputSource(reader));
        } catch (Exception e) {
            throw new RuntimeException("Could not parse DOM for '" + reader.toString() + "'!", e);
        }
    }
}

Related

  1. parse_XML_String_and_create_DOM(String xmlData)
  2. parseDom(InputStream byteArrayInputStream)
  3. parseDom(Reader reader)
  4. parseDOMConfigFile(String folderPath, String configFileName)
  5. parseFileToXML(Transformer transformer, DOMSource source, File file)
  6. parseToDOM(InputStream stream)
  7. parseToDom(String content)
  8. parseXMLToDOM(String xml_str)