Java XML DocumentBuilder Create newDocumentBuilder()

Here you can find the source of newDocumentBuilder()

Description

Construct a non-validating document builder.

License

Open Source License

Return

a new document builder

Declaration

static DocumentBuilder newDocumentBuilder() 

Method Source Code

//package com.java2s;
/*//from   w w  w  .j  a v a2 s.  c o m
 * Copyright (c) 2017 Public Library of Science
 *
 * 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.
 */

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

public class Main {
    /**
     * Construct a non-validating document builder. We assume that we don't want to connect to remote servers to validate
     * except with a specific reason.
     *
     * @return a new document builder
     */
    static DocumentBuilder newDocumentBuilder() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // at a minimum the document builder needs to be namespace aware
        factory.setNamespaceAware(true);
        factory.setValidating(false);
        try {
            factory.setFeature("http://xml.org/sax/features/validation", false);
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            return factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. getDocumentBuilderFactory(boolean isNamespaceAware)
  2. getDocumentBuilderFactory(String schema)
  3. getDocumentBuilderFactoryClassName()
  4. getNewDocumentBuilder()
  5. getNewDocumentBuilder()
  6. newDocumentBuilder()
  7. newDocumentBuilder()
  8. newDocumentBuilder()
  9. newDocumentBuilder()