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

public static Document parse(String xmlStr) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);//w  ww  . java  2  s . co  m

    factory.setNamespaceAware(false);

    factory.setIgnoringComments(true);

    factory.setIgnoringElementContentWhitespace(false);

    factory.setCoalescing(false);

    factory.setExpandEntityReferences(true);

    DocumentBuilder builder = factory.newDocumentBuilder();

    ByteArrayInputStream inputStream = new ByteArrayInputStream(xmlStr.getBytes());

    return builder.parse(inputStream);

}

From source file:Main.java

/**
 * Loads an XML document from an InputStream
 *//*from w  w  w .  j a va2  s.c o m*/
public static Document loadXmlDoc(final InputStream stream) {
    Document result = null;
    try {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setExpandEntityReferences(false);
        domFactory.setIgnoringComments(true);//strips comments
        //         domFactory.setIgnoringElementContentWhitespace(true);//would be nice if it worked
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        result = builder.parse(stream);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

/**
 * /*from ww w  .  ja v a 2  s  .  c o m*/
 */
public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    factory.setCoalescing(true);
    factory.setNamespaceAware(false);

    return factory.newDocumentBuilder();
}

From source file:Main.java

static Document getLoadingDoc(InputStream in) throws SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setValidating(false);//  w  w w  .ja  v  a  2  s . c om
    dbf.setCoalescing(true);
    dbf.setIgnoringComments(true);
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(in);
        return db.parse(is);
    } catch (ParserConfigurationException x) {
        throw new Error(x);
    }
}

From source file:org.datacleaner.util.http.HttpXmlUtils.java

public static DocumentBuilder createDocumentBuilder() {
    try {//from ww  w  .j  a  v a  2  s .  co m
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringComments(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        return db;
    } catch (Exception e) {
        // This shouldn't be possible
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String toNormalizedXML(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from   w  w w .j  a  va  2  s.  c  o m*/
    dbf.setCoalescing(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(is);
    document.normalizeDocument();
    document.getDocumentElement().setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", //$NON-NLS-1$
            "xsi:schemaLocation", //$NON-NLS-1$
            "http://abc4trust.eu/wp2/abcschemav1.0 ../../../../../../../../../abc4trust-xml/src/main/resources/xsd/schema.xsd"); //$NON-NLS-1$
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    String xml = serializer.writeToString(document);

    return trim(xml);
}

From source file:Main.java

public static boolean compareXmls(InputStream xml1, InputStream xml2)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);// ww w . j a va  2s  . c  om
    dbf.setCoalescing(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc1 = db.parse(xml1);
    doc1.normalizeDocument();

    Document doc2 = db.parse(xml2);
    doc2.normalizeDocument();

    return doc2.isEqualNode(doc1);
}

From source file:Main.java

/**
 * Creates a new {@link DocumentBuilder} object.
 *///from w w w.j  a  v a 2  s.co  m
private static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    factory.setIgnoringComments(true);
    factory.setExpandEntityReferences(false);
    factory.setCoalescing(false);

    factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

    return factory.newDocumentBuilder();
}

From source file:com.synclio.hawk.TutorialRouteBuilder.java

private static DocumentBuilderFactory createDocumentBuilderFactory() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from   w  w w.  j av a  2s  .  c o  m
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    return factory;
}

From source file:com.connexta.arbitro.ctx.InputParser.java

/**
 * Tries to Parse the given output as a Context document.
 * /*from  ww w  . j av  a2s. c  o m*/
 * @param input the stream to parse
 * @param rootTag either "Request" or "Response"
 * 
 * @return the root node of the request/response
 * 
 * @throws ParsingException if a problem occurred parsing the document
 */
public static Node parseInput(InputStream input, String rootTag) throws ParsingException {
    NodeList nodes = null;

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);

        DocumentBuilder builder = null;

        // as of 1.2, we always are namespace aware
        factory.setNamespaceAware(true);

        if (ipReference == null) {
            // we're not validating
            factory.setValidating(false);

            builder = factory.newDocumentBuilder();
        } else {
            // we are validating
            factory.setValidating(true);

            factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
            factory.setAttribute(JAXP_SCHEMA_SOURCE, ipReference.schemaFile);

            builder = factory.newDocumentBuilder();
            builder.setErrorHandler(ipReference);
        }

        Document doc = builder.parse(input);
        nodes = doc.getElementsByTagName(rootTag);
    } catch (Exception e) {
        throw new ParsingException("Error tring to parse " + rootTag + "Type", e);
    }

    if (nodes.getLength() != 1)
        throw new ParsingException("Only one " + rootTag + "Type allowed " + "at the root of a Context doc");

    return nodes.item(0);
}