Parse the XML file and create Document - Java XML

Java examples for XML:DOM Document

Description

Parse the XML file and create Document

Demo Code

/************************************************************************
 MIT License/*from   w  w  w.  j  av a2  s . co m*/

 Copyright (c) 2010 University of Connecticut

 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ***********************************************************************/
//package com.java2s;
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;

public class Main {
    /**
     * Parse the XML file and create Document
     * @param fileName
     * @return Document
     */
    public static Document parse(String fileName) throws Exception {
        Document document = null;
        // Initiate DocumentBuilderFactory
        DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();

        // To get a validating parser
        factory.setValidating(false);
        // To get one that understands namespaces
        factory.setNamespaceAware(true);

        //try {
        // Get DocumentBuilder
        DocumentBuilder builder = factory.newDocumentBuilder();
        // Parse and load into memory the Document
        document = builder.parse(new File(fileName));
        return document;
        /*
        } catch (SAXParseException spe) {
        // Error generated by the parser
        System.out.println("\n** Parsing error , line " + spe.getLineNumber()
                           + ", uri " + spe.getSystemId());
        System.out.println(" " + spe.getMessage() );
        // Use the contained exception, if any
        Exception x = spe;
        if (spe.getException() != null)
          x = spe.getException();
        x.printStackTrace();
        } catch (SAXException sxe) {
        // Error generated during parsing
        Exception x = sxe;
        if (sxe.getException() != null)
          x = sxe.getException();
        x.printStackTrace();
        } catch (ParserConfigurationException pce) {
        // Parser with specified options can't be built
        pce.printStackTrace();
        } catch (IOException ioe) {
        // I/O error
        ioe.printStackTrace();
        }

        return null; //*/
    }
}

Related Tutorials