Here you can find the source of parseDomFromFile(File doc)
Parameter | Description |
---|---|
doc | the xml file |
public static Document parseDomFromFile(File doc)
//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); } } }