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:Main.java

/**
 * load a xml file from OS file system and interpret it into a Document no
 * charset limited/*from ww  w  .  java2s .  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  w ww . ja  va2s  . c o m*/
 * 
 * @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:Main.java

/** Read XML as DOM.
 *///from ww  w  .ja va2  s. c  om
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

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

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

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

    Document doc = db.parse(is);
    return doc;
}

From source file:Main.java

public static Document getXmlDoc(String xmlFile)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setIgnoringComments(true);
    domFactory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(new File(xmlFile));
    return doc;/*from   www.j  a v  a  2s.c om*/
}

From source file:Main.java

/**
 * To parse an input stream to DOM/*from  w w w . j av a2s. c  o 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);/* ww w  .j a v a2  s  .  c om*/
    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:Main.java

private static DocumentBuilderFactory newDocumentBuilderFactory() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setExpandEntityReferences(false);
    return dbf;/* ww  w.j  a  v a2  s  .com*/
}

From source file:Main.java

public static Document getDocument(String file_path) {
    try {/*from   www .j av a 2 s . com*/
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setIgnoringComments(true);
        DocumentBuilder builder;
        builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(new File(file_path));

        return doc;

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

private static DocumentBuilderFactory createDocumentBuilderFactory() {
    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    // Assume default processing is too strict
    f.setValidating(false);/*  w w w.  j  a  va2  s.  c  om*/
    f.setIgnoringComments(true);
    f.setNamespaceAware(true);

    return f;
}

From source file:Main.java

/**
 * Returns a default DocumentBuilder instance or throws an
 * ExceptionInInitializerError if it can't be created.
 *
 * @return a default DocumentBuilder instance.
 *//*from  ww  w . j  av a2  s .  co m*/
public static DocumentBuilder getDocumentBuilder() throws Exception {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringComments(true);
        dbf.setCoalescing(true);
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setValidating(false);
        return dbf.newDocumentBuilder();
    } catch (Exception exc) {
        throw new Exception(exc.getMessage());
    }
}