Example 1 - creating DOM tree from file

Example of method, reading a DOM tree from an XML file (see Home work 1):

import java.io.IOException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Uloha1 {

    /**
     * Constructor creating new instance of Uloha1 class by reading XML document
     * on the given URL.
     */
    private Uloha1(URL url) throws SAXException, ParserConfigurationException, IOException {
        // We create new instance of factory class
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // We get new instance of DocumentBuilder using the factory class.
        DocumentBuilder builder = factory.newDocumentBuilder();
        // We utilize the DocumentBuilder to process an XML document
        // and we get document model in form of W3C DOM
        Document doc = builder.parse(url.toString());
    }
}