Example usage for javax.xml.parsers DocumentBuilderFactory setValidating

List of usage examples for javax.xml.parsers DocumentBuilderFactory setValidating

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory setValidating.

Prototype


public void setValidating(boolean validating) 

Source Link

Document

Specifies that the parser produced by this code will validate documents as they are parsed.

Usage

From source file:Main.java

public static Document parseXmlFile(String filename, boolean validating) {
    try {//from w w w . jav  a 2 s.com
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validating);
        // Create the builder and parse the file
        Document doc = factory.newDocumentBuilder().parse(new File(filename));
        return doc;
    } catch (SAXException e) {
        // A parsing error occurred; the xml input is not valid
        System.err.println("SAXException in parsing XML File:" + filename + ", will return null doc");
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        System.err.println(
                "ParserConfigurationException in parsing XML File:" + filename + ", will return null doc");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException in parsing XML File:" + filename + ", will return null doc");
        e.printStackTrace();
    }
    return null;
}

From source file:Utils.java

/**
 * Read XML as DOM.//from  ww w . j a  va  2s .c o m
 */
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());

    return db.parse(is);
}

From source file:Main.java

public static Document parseXmlData(String xmlData, boolean validating) {

    try {/* w  w  w .ja v a  2s .  c  o m*/
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validating);
        // Create the builder and parse the file
        Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xmlData)));
        return doc;
    } catch (SAXException e) {
        // A parsing error occurred; the xml input is not valid
        System.err.println("SAXException in parsing XML data, will return null doc");
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        System.err.println("ParserConfigurationException in parsing XML data, will return null doc");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException in parsing XML data, will return null doc");
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

/** 
 * Create from factory a DocumentBuilder and let it create a org.w3c.dom.Document.
 * This method takes InputSource. After successful finish the document tree is returned.
 *
 * @param input a parser input (for URL users use: <code>new InputSource(url.toExternalForm())</code>
 * @param validate if true validating parser is used
 * @param namespaceAware if true DOM is created by namespace aware parser
 * @param errorHandler a error handler to notify about exception or <code>null</code>
 * @param entityResolver SAX entity resolver or <code>null</code>; see class Javadoc for hints
 *
 * @throws IOException if an I/O problem during parsing occurs
 * @throws SAXException is thrown if a parser error occurs
 * @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type.
 *
 * @return document representing given input, or null if a parsing error occurs
 *//*from ww  w .j  av  a2 s.  c  o  m*/
public static Document parse(InputSource input, boolean validate, boolean namespaceAware,
        ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validate);
    factory.setNamespaceAware(namespaceAware);

    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N
    }

    if (errorHandler != null) {
        builder.setErrorHandler(errorHandler);
    }

    if (entityResolver != null) {
        builder.setEntityResolver(entityResolver);
    }

    return builder.parse(input);
}

From source file:Main.java

public static DocumentBuilder newDocumentBuilder(Boolean disallowDoctypeDecl)
        throws ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from w w w.j a va2  s.co  m
    dbf.setValidating(false);
    // avoid external entity attacks
    dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
    dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    boolean isDissalowDoctypeDecl = disallowDoctypeDecl == null ? true : disallowDoctypeDecl;
    dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", isDissalowDoctypeDecl);
    // avoid overflow attacks
    dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

    return dbf.newDocumentBuilder();
}

From source file:Utils.java

public static Document readXml(StreamSource is) throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);//from   ww w .  j  a  va 2s.  c om
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());
    InputSource is2 = new InputSource();
    is2.setSystemId(is.getSystemId());
    is2.setByteStream(is.getInputStream());
    is2.setCharacterStream(is.getReader());

    return db.parse(is2);
}

From source file:Main.java

/**
 * Boilerplate code for creating Java's XML DOM parser.
 *//*  ww w .j av a 2  s.c  o m*/
private static DocumentBuilder newBuilder() {
    try {
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        fac.setNamespaceAware(false);
        fac.setValidating(false);
        DocumentBuilder builder = fac.newDocumentBuilder();
        return builder;
    } catch (ParserConfigurationException pce) {
        throw new RuntimeException(pce);
    }
}

From source file:Main.java

private static Element loadRootElement(InputStream inputStream) throws Exception {
    // Get XML document from the URL
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    //as a default, set validating to false
    factory.setValidating(false);
    factory.setNamespaceAware(true);/* www. ja  v a  2  s. c  om*/
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource inputSource = new InputSource(inputStream);
    Document doc = builder.parse(inputSource);
    return doc.getDocumentElement();
}

From source file:Main.java

public static DocumentBuilder newDocumentBuilder(boolean validating) {

    try {//from   ww  w.ja  va 2  s .  c o  m

        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        dbfac.setNamespaceAware(true);
        dbfac.setValidating(validating);

        return dbfac.newDocumentBuilder();

    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Create a new XML Document.//  w  ww  .  j  a v a  2  s . co  m
 * @param docBuilder DocumentBuilder. Setting this to null will create a new DocumentBuilder.
 * @return Document
 */
public static final Document createNewDocument(DocumentBuilder docBuilder) throws ParserConfigurationException {
    if (docBuilder == null) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        docBuilder = factory.newDocumentBuilder();
    }
    Document doc = docBuilder.newDocument();
    return doc;
}