Java XML Parse File parse(File xml, ErrorHandler errorHandler)

Here you can find the source of parse(File xml, ErrorHandler errorHandler)

Description

Parses the.

License

Apache License

Parameter

Parameter Description
xml the xml
errorHandler the error handler

Exception

Parameter Description
ParserConfigurationException the parser configuration exception
SAXException the sAX exception
IOException Signals that an I/O exception has occurred.

Return

the document

Declaration

public static Document parse(File xml, ErrorHandler errorHandler)
        throws ParserConfigurationException, SAXException, IOException 

Method Source Code

//package com.java2s;
/**/* w  w w  . jav a  2 s  .  c o m*/
 * Copyright (C) 2007 Asterios Raptis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
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.ErrorHandler;
import org.xml.sax.SAXException;

public class Main {
    /** The Constant DOCUMENT_BUILDER_FACTORY_VALUE. */
    private static final String DOCUMENT_BUILDER_FACTORY_VALUE = "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl";
    /** The Constant DOCUMENT_BUILDER_FACTORY_KEY. */
    private static final String DOCUMENT_BUILDER_FACTORY_KEY = "javax.xml.parsers.DocumentBuilderFactory";
    /** The Constant SCHEMA_SOURCE_KEY. */
    private static final String SCHEMA_SOURCE_KEY = "http://java.sun.com/xml/jaxp/properties/schemaSource";
    /** The Constant SCHEMA_LANGUAGE_KEY. */
    private static final String SCHEMA_LANGUAGE_KEY = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
    /** The Constant HTTP_WWW_W3_ORG_2001_XML_SCHEMA. */
    private static final String HTTP_WWW_W3_ORG_2001_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";

    /**
     * Parses the.
     *
     * @param xml
     *            the xml
     * @param errorHandler
     *            the error handler
     * @return the document
     * @throws ParserConfigurationException
     *             the parser configuration exception
     * @throws SAXException
     *             the sAX exception
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Document parse(File xml, ErrorHandler errorHandler)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = getDocumentBuilderFactory(xml.getName());
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(errorHandler);
        return builder.parse(xml);
    }

    /**
     * Gets the document builder factory.
     *
     * @param schema
     *            the schema
     * @return the document builder factory
     */
    public static DocumentBuilderFactory getDocumentBuilderFactory(String schema) {
        System.setProperty(DOCUMENT_BUILDER_FACTORY_KEY, DOCUMENT_BUILDER_FACTORY_VALUE);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(true);
        factory.setAttribute(SCHEMA_LANGUAGE_KEY, HTTP_WWW_W3_ORG_2001_XML_SCHEMA);
        factory.setAttribute(SCHEMA_SOURCE_KEY, schema);
        return factory;
    }
}

Related

  1. parse(File file)
  2. parse(File file)
  3. parse(File file)
  4. parse(File iFile)
  5. parse(File input)
  6. parse(String filename)
  7. parse(String fileName)
  8. parse(String fileName)
  9. parse(String fileName)