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(File f, boolean validating)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validating);
    Document doc = factory.newDocumentBuilder().parse(f);
    return doc;/*from  w  w w  .j av  a2 s . co m*/
}

From source file:Main.java

public static Document parseXmlFile(InputStream stream, boolean validating)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validating);
    Document doc = factory.newDocumentBuilder().parse(stream);
    return doc;/*from   w ww.  j a  va2 s  .c  om*/
}

From source file:Main.java

/**
 * Parses a document from the given string
 * /*ww w .j  a  v  a2s  .co m*/
 * @param template The string to parse
 * @return The parsed {@link Document}
 */
public static Document getTemplateDocument(String template) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;
    dbf.setValidating(false);
    try {
        db = dbf.newDocumentBuilder();
        db.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicID, String systemID) throws SAXException {
                return new InputSource(new StringReader(""));
            }
        });
        Document doc = db.parse(new ByteArrayInputStream(template.getBytes("utf8")));
        return doc;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Element stringToElement(String str) {
    StringReader sr = new StringReader(str);

    try {//  w w  w. jav  a  2 s .co m
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(false);
        docBuilderFactory.setValidating(false);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        InputSource is = new InputSource(sr);
        // is.setEncoding("gb2312");
        Document doc = docBuilder.parse(is);
        return doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Returns a <code>DocumentBuilder</code>, which is used for parsing XML
 * documents./*from www. jav a2 s.c o m*/
 * 
 * @return a <code>DocumentBuilder</code> which is used for parsing XML
 *         documents. Never <code>null</code>.
 */
public static DocumentBuilder getDocumentBuilder() {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(false);

        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
}

From source file:Main.java

/**
 *
 * @param xmlContent/*from w w w. j a v  a2  s.co  m*/
 * @param charset
 * @return
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
private static Document parse(String xmlContent, Charset charset)
        throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(false);
    documentBuilderFactory.setValidating(false);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes(charset)));

    return document;
}

From source file:Main.java

/**
 * Valida un documento XML con un esquema XML (XSD).
 * @param xml File to validate/*from ww w. j av  a  2 s . co  m*/
 * @param xmlSchema File with the schema
 * @return Dom document
 * @throws ParserConfigurationException 
 * @throws SAXException 
 * @throws IOException  
 */
public static Document validate(File xml, File xmlSchema)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setNamespaceAware(true);
    factory.setValidating(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xmlSchema);

    // Parsing 
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    documentBuilder.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException ex) throws SAXException {
            throw ex;
        }

        public void error(SAXParseException ex) throws SAXException {
            throw ex;
        }

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

    return documentBuilder.parse(xml);
}

From source file:Main.java

public static DocumentBuilder createParser() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    try {/*w ww.ja v a 2 s.  co  m*/
        dbf.setNamespaceAware(true);
        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);
    } catch (ParserConfigurationException ex) {
        throw new RuntimeException(ex);
    }

    try {
        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        System.err.println("[error] XmlUtils: Cannot create a DocumentBuilder instance.");
    }

    return null;
}

From source file:Main.java

public static Document getDoc(String xml) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;/*from  www . jav  a2  s  .  c  om*/

    dbf.setValidating(false);
    try {
        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);
        db = dbf.newDocumentBuilder();
        InputSource source = new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")));
        Document doc = db.parse(source);
        return doc;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Element byteArrayToElement(byte[] cont) {
    if (cont == null || cont.length <= 0)
        return null;

    try {// w  ww  . java 2  s  .  c o  m
        ByteArrayInputStream bais = new ByteArrayInputStream(cont);
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(false);
        docBuilderFactory.setValidating(false);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        InputSource is = new InputSource(bais);
        // is.setEncoding("gb2312");
        Document doc = docBuilder.parse(is);
        return doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}