Example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringComments

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

Introduction

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

Prototype


public void setIgnoringComments(boolean ignoreComments) 

Source Link

Document

Specifies that the parser produced by this code will ignore comments.

Usage

From source file:Utils.java

public static Document readXml(Reader is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);/*w w  w .  j  a  va2s  . c  o m*/
    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());
    InputSource ips = new InputSource(is);
    return db.parse(ips);
}

From source file:Main.java

public static Document parse(File file) throws IOException {
    try {//  w ww .j  a  va 2 s. c o m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setCoalescing(true);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(true);
        DocumentBuilder parser = factory.newDocumentBuilder();
        return parser.parse(file);
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static Document parse(InputStream is) throws IOException {
    try {//from w  w  w . ja va  2s .co m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setCoalescing(true);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(true);
        DocumentBuilder parser = factory.newDocumentBuilder();
        InputSource in = new InputSource(is);
        return parser.parse(in);
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    }
}

From source file:Utils.java

/**
 * Read XML as DOM.//from ww w  .j  a v  a2 s.  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

private static DocumentBuilderFactory createDocumentBuilderFactory() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//  w  w  w  .ja va2 s .c o  m
    factory.setCoalescing(true);
    factory.setIgnoringComments(true);
    return factory;
}

From source file:Utils.java

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

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);//from   ww  w.j a  v a 2 s.  c om
    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());
    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

/**
 * @param xmlStr// ww w .j  ava 2 s .  c o  m
 * @return the Document or null upon error.
 */
public static Document createDocumentFromXmlString(String xmlStr) {
    try {
        DocumentBuilderFactory docBuilderFactory;
        docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setIgnoringComments(true);
        docBuilderFactory.setIgnoringElementContentWhitespace(true);
        docBuilderFactory.setNamespaceAware(false);
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        ByteArrayInputStream bais = new ByteArrayInputStream(xmlStr.getBytes());
        Document doc = builder.parse(bais);
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Document CreateDocument(Path xml, Path xsd)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from  w w  w. j  ava 2s. co m*/
    factory.setNamespaceAware(true);
    factory.setIgnoringComments(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    if (xsd != null)
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xsd.toString());
    return factory.newDocumentBuilder().parse(xml.toString());
}

From source file:Main.java

public static Document CreateDocument(Path xml, Path xsd)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//  w ww  . j a va  2  s  .c  o m
    factory.setNamespaceAware(true);
    factory.setIgnoringComments(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    if (xsd != null) {
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xsd.toString());
    }
    return factory.newDocumentBuilder().parse(xml.toString());
}

From source file:Main.java

private static Document toDocument(InputSource in) throws SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//w w w  .  ja  va 2  s.  co m
    factory.setIgnoringComments(true);

    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        //will probably never be thrown because we're not doing anything fancy with the configuration
        throw new RuntimeException(e);
    }

    return builder.parse(in);
}