Java XML Parse File parse(String filename)

Here you can find the source of parse(String filename)

Description

Parse an XML document using Saxon.

License

Open Source License

Parameter

Parameter Description
filename The file name of the xml file to be read in The filename is passed to org.xml.sax.InputSource(String), so it may be a file name or a URL.

Return

the parsed document.

Declaration

public static Document parse(String filename)
        throws ParserConfigurationException, IOException 

Method Source Code

//package com.java2s;
/* Utilities that manipulate strings using XSLT.

 Copyright (c) 2002-2013 The Regents of the University of California.
 All rights reserved.//w w  w  . j  av a 2s  . c o m
 Permission is hereby granted, without written agreement and without
 license or royalty fees, to use, copy, modify, and distribute this
 software and its documentation for any purpose, provided that the above
 copyright notice and the following two paragraphs appear in all copies
 of this software.

 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 ENHANCEMENTS, OR MODIFICATIONS.

 PT_COPYRIGHT_VERSION_2
 COPYRIGHTENDKEY

 */

import java.io.IOException;

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

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    /** Parse an XML document using Saxon.
     *  @param filename The file name of the xml file to be read in
     *  The filename is passed to org.xml.sax.InputSource(String),
     *  so it may be a file name or a URL.
     *  @return the parsed document.
     *  @exception ParserConfigurationException If there is a problem
     *  creating the DocumentBuilder.
     *  @exception IOException If the filename could not be parsed.
     */
    public static Document parse(String filename)
            throws ParserConfigurationException, IOException {
        // FIXME: Throw something other than Exception
        //        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
        //                "net.sf.saxon.om.DocumentBuilderFactoryImpl");

        DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        // We use InputSource here so that we can specify the filename
        // argument as a jar url so that HSIFToMoML works under Web Start.
        try {
            return builder.parse(new InputSource(filename));
        } catch (SAXException ex) {
            // Rethrow this with the filename included.
            IOException exception = new IOException("Failed to parse '"
                    + filename + "'");
            exception.initCause(ex);
            throw exception;
        }
    }
}

Related

  1. parse(File iFile)
  2. parse(File input)
  3. parse(File xml, ErrorHandler errorHandler)
  4. parse(String fileName)
  5. parse(String fileName)
  6. parse(String filename)
  7. parse(String fileName)
  8. parse(String fileName)
  9. parse(String xmlFile)