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 void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setIgnoringComments(true);

    Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml"));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setIgnoringComments(true);

    Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml"));
    // doc will not contain any Comment nodes
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File CFile = new File("data.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(false);//  w  w  w. jav  a2  s  . c  o  m
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(CFile);

    NodeList pizzas = document.getElementsByTagName("Pizza");

    for (int i = 0; i < pizzas.getLength(); i++) {
        Element pizzaSize = (Element) pizzas.item(i);
        String pSize = pizzaSize.getAttribute("Size");

        if (pSize.equalsIgnoreCase("Large"))
            System.out.println(10.0);
        if (pSize.equalsIgnoreCase("Medium"))
            System.out.println(7.0);
        if (pSize.equalsIgnoreCase("Small"))
            System.out.println(5.0);
    }
}

From source file:MainClass.java

public static void main(String[] args)
        throws IOException, ParserConfigurationException, org.xml.sax.SAXException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);
    factory.setCoalescing(true); // Convert CDATA to Text nodes
    factory.setNamespaceAware(false); // No namespaces: this is default
    factory.setValidating(false); // Don't validate DTD: also default

    DocumentBuilder parser = factory.newDocumentBuilder();

    Document document = parser.parse(new File(args[0]));

    NodeList sections = document.getElementsByTagName("sect1");
    int numSections = sections.getLength();
    for (int i = 0; i < numSections; i++) {
        Element section = (Element) sections.item(i); // A <sect1>

        Node title = section.getFirstChild();
        while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
            title = title.getNextSibling();

        if (title != null)
            System.out.println(title.getFirstChild().getNodeValue());
    }/*from   w  ww.  j  ava2s .c o m*/
}

From source file:ListMoviesXML.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);

    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(true);/*from  w w  w .j  a v a 2s.c om*/
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse(new InputSource("y.xml"));
    Element root = doc.getDocumentElement();

    Element movieElement = (Element) root.getFirstChild();
    Movie m;
    while (movieElement != null) {
        m = getMovie(movieElement);
        String msg = Integer.toString(m.year);
        msg += ": " + m.title;
        msg += " (" + m.price + ")";
        System.out.println(msg);
        movieElement = (Element) movieElement.getNextSibling();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setValidating(false);/*from   w  w  w. j  a  v a  2s . c om*/
    domFactory.setNamespaceAware(true);
    domFactory.setIgnoringComments(true);
    domFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document dDoc = builder.parse("C:/data.xsd");

    Node rootNode = dDoc.getElementsByTagName("xs:schema").item(0);
    System.out.println(rootNode.getNodeName());

    XPath xPath1 = XPathFactory.newInstance().newXPath();
    NamespaceContext nsContext = new NamespaceContext() {
        @Override
        public String getNamespaceURI(String prefix) {
            return "http://www.w3.org/2001/XMLSchema";
        }

        @Override
        public String getPrefix(String namespaceURI) {
            return "xs";
        }

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            Set s = new HashSet();
            s.add("xs");
            return s.iterator();
        }
    };
    xPath1.setNamespaceContext((NamespaceContext) nsContext);
    NodeList nList1 = (NodeList) xPath1.evaluate("//xs:schema", dDoc, XPathConstants.NODESET);
    System.out.println(nList1.item(0).getNodeName());

    NodeList nList2 = (NodeList) xPath1.evaluate("//xs:element", rootNode, XPathConstants.NODESET);
    System.out.println(nList2.item(0).getNodeName());
}

From source file:Main.java

public static DocumentBuilderFactory newDocumentBuilderFactory() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);
    return factory;
}

From source file:Main.java

private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringComments(true);
    dbf.setCoalescing(true);/*www. ja  va 2 s  . c  o m*/
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setValidating(false);
    return dbf.newDocumentBuilder();
}

From source file:Main.java

public static Document load(File fileName) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(false);/*  w ww. j av  a  2s  .c o m*/
    factory.setCoalescing(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(fileName);
}

From source file:Main.java

/**
 * Creates clean Document used in other classes for working with XML
 *
 * @return clean Document/*from   w w w . j a  v a2s  .  c  om*/
 * @throws ParserConfigurationException if creation of document fails
 */
public static Document createDoc() throws ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setIgnoringComments(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.getDOMImplementation().createDocument(null, null, null);
    return doc;
}