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 toDocument(String xml, boolean namespaceAware, boolean ignoreDtd) throws Exception {
    Reader input = new StringReader(xml);
    InputSource is = new InputSource(input);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(namespaceAware);

    // ignore dtd files
    if (ignoreDtd) {
        dbf.setValidating(false);
        dbf.setFeature("http://xml.org/sax/features/namespaces", false);
        dbf.setFeature("http://xml.org/sax/features/validation", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    }/*  w  w w  .ja  v  a  2 s . c om*/

    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException exception) throws SAXException {
            throw exception;

        }

        public void fatalError(SAXParseException exception) throws SAXException {
            throw exception;
        }

        public void error(SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    return db.parse(is);
}

From source file:Main.java

public static Document blankDocument(String paramString) throws Exception {
    DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    localDocumentBuilderFactory.setIgnoringComments(false);
    localDocumentBuilderFactory.setIgnoringElementContentWhitespace(false);
    localDocumentBuilderFactory.setValidating(false);
    localDocumentBuilderFactory.setCoalescing(false);
    DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
    Document localDocument = localDocumentBuilder.newDocument();
    Element localElement = localDocument.createElement(paramString);
    localDocument.appendChild(localElement);
    return localDocument;
}

From source file:Main.java

/**
 * load a xml file from OS file system and interpret it into a Document no
 * charset limited//  www .j a  v a 2s.  c o  m
 * 
 * @param xmlfile
 * @return Document
 * @throws Exception
 */
public static Document load(String xmlfile) throws Exception {
    javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(xmlfile);
}

From source file:Main.java

/**
 * load a xml file from OS file system and interpret it into a Document no
 * charset limited//from www. jav a 2  s. com
 * 
 * @param xmlfile
 *            String
 * @return Document
 * @throws Exception
 */
public static Document load(File xmlfile) throws Exception {
    javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(xmlfile);
}

From source file:org.earthtime.archivingTools.IEDACredentialsValidator.java

private static Document HTTP_PostAndResponse(String userName, String password, String credentialsService) {
    Document doc = null;//from  w w w .j  av  a 2 s  . c  o  m
    Map<String, String> dataToPost = new HashMap<>();
    dataToPost.put("username", userName);
    dataToPost.put("password", password);

    CloseableHttpClient httpclient = HttpClients.createDefault();
    org.apache.http.client.methods.HttpPost httpPost = new HttpPost(credentialsService);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("username", userName));
    nameValuePairs.add(new BasicNameValuePair("password", password));
    CloseableHttpResponse httpResponse = null;
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        httpResponse = httpclient.execute(httpPost);
        HttpEntity myEntity = httpResponse.getEntity();
        InputStream response = myEntity.getContent();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        try {
            doc = factory.newDocumentBuilder().parse(response);
            //System.out.println("CCCC" + doc.getElementsByTagName("valid").item(0).getTextContent().trim());
        } catch (ParserConfigurationException | SAXException | IOException parserConfigurationException) {
            System.out.println("PARSE error " + parserConfigurationException.getMessage());
        }

        EntityUtils.consume(myEntity);
    } catch (IOException iOException) {

    } finally {
        try {
            httpResponse.close();
        } catch (IOException iOException) {
        }
    }

    return doc;
}

From source file:Main.java

/**
 * To parse an input stream to DOM/*from  w  ww .  jav a 2  s.co m*/
 * <p/>
 * This implementation attempts to preserve the xml structure as-is with whitespace etc
 */
public static Document inputStreamToDocument(InputStream is) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setExpandEntityReferences(false);
    factory.setIgnoringComments(false);
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);

    DocumentBuilder builder = factory.newDocumentBuilder();
    Document root = builder.parse(is);

    return root;
}

From source file:Main.java

public static Document createDocument(Reader reader) throws IllegalArgumentException {
    // init DOM builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setCoalescing(true);//  w w w.j av a 2s.  co  m
    factory.setIgnoringComments(true);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(false);

    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource inputSource = new InputSource(reader);
        Document doc = builder.parse(inputSource);
        return doc;
    } catch (Exception ex) {
        IllegalArgumentException iae = new IllegalArgumentException(ex.getMessage());
        iae.initCause(ex);
        throw iae;
    }
}

From source file:com.twentyn.patentExtractor.Util.java

public static DocumentBuilderFactory mkDocBuilderFactory() throws ParserConfigurationException {
    /* Try to load the document.  Note that the factory must be configured within the context of a method call
     * for exception handling.  TODO: can we work around this w/ dependency injection? */

    // With help from http://stackoverflow.com/questions/155101/make-documentbuilder-parse-ignore-dtd-references
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setValidating(false);
    docFactory.setNamespaceAware(true);//from w  w  w .  ja  va2 s.  co  m
    docFactory.setFeature("http://xml.org/sax/features/namespaces", false);
    docFactory.setFeature("http://xml.org/sax/features/validation", false);
    docFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    docFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    return docFactory;
}

From source file:info.magnolia.cms.util.ConfigUtil.java

/**
 * Uses a map to find dtds in the resources.
 * @param xml// w ww .ja va 2  s.  c  om
 * @param dtds
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document string2DOM(String xml, final Map<String, String> dtds)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);
    DocumentBuilder builder;
    builder = dbf.newDocumentBuilder();
    builder.setEntityResolver(new MapDTDEntityResolver(dtds));
    return builder.parse(IOUtils.toInputStream(xml));
}

From source file:net.sf.jasperreports.engine.util.JRXmlUtils.java

/**
 * Creates a XML document builder./*from w ww  .  java  2s .c o m*/
 * 
 * @return a XML document builder
 * @throws JRException
 */
public static DocumentBuilder createDocumentBuilder(boolean isNamespaceAware) throws JRException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setIgnoringComments(true);
    dbf.setNamespaceAware(isNamespaceAware);
    try {
        if (!allowDoctype()) {
            dbf.setFeature(FEATURE_DISALLOW_DOCTYPE, true);
        }

        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_DOCUMENT_BUILDER_FACTORY_CREATION_FAILURE, null, e);
    }
}