Java tutorial
//package com.java2s; /*--------------------------------------------------------------- * Copyright 2005 by the Radiological Society of North America * * This source software is released under the terms of the * RSNA Public License (http://mirc.rsna.org/rsnapubliclicense) *----------------------------------------------------------------*/ import java.io.*; import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; public class Main { /** * Parses an XML file. * @param realFilePath the path to the file containing the XML to parse. * @return the XML DOM document. */ public static Document getDocument(String realFilePath) throws Exception { File file = new File(realFilePath); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return db.parse(file); } /** * Parses an XML file. * @param file the file containing the XML to parse. * @return the XML DOM document. */ public static Document getDocument(File file) throws Exception { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return db.parse(file); } /** * Creates a new empty XML DOM document. * @return the XML DOM document. */ public static Document getDocument() throws Exception { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return db.newDocument(); } }