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

static public void main(String[] arg) {
    boolean validate = false;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);
    dbf.setNamespaceAware(true);//from   w w  w  .  j  a va 2  s .c  o  m
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(xmlString)));
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }

    TreeDumper td = new TreeDumper();
    td.dump(doc);
}

From source file:Main.java

static public void main(String[] arg) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);
    dbf.setNamespaceAware(true);//w w  w  . j a  v a 2 s  .c  o m
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();

        InputSource is = new InputSource(new StringReader(getXMLData()));
        doc = builder.parse(is);

        write(doc);
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:DOMCheck.java

static public void main(String[] arg) {
    String filename = null;//from   www . j a v a 2  s .  co m
    boolean validate = false;

    if (arg.length == 1) {
        filename = arg[0];
    } else if (arg.length == 2) {
        if (!arg[0].equals("-v"))
            usage();
        validate = true;
        filename = arg[1];
    } else {
        usage();
    }

    // Create a new factory to create parsers that will
    // be aware of namespaces and will validate or
    // not according to the flag setting.
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);
    dbf.setNamespaceAware(true);

    // Use the factory to create a parser (builder) and use
    // it to parse the document.
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is = new InputSource(filename);
        Document doc = builder.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:DOMDump.java

static public void main(String[] arg) {
    boolean validate = true;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);
    dbf.setNamespaceAware(true);//from  ww w.j a  v a2s . co m
    dbf.setIgnoringElementContentWhitespace(true);

    // Parse the input to produce a parse tree with its root
    // in the form of a Document object
    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is = new InputSource("personWithDTD.xml");
        doc = builder.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
    dump(doc);
}

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);//ww  w . j  av  a  2 s .c  o m
    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(false);
    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:DOMCopy.java

static public void main(String[] arg) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);
    dbf.setNamespaceAware(true);// w w w  .j  a v  a  2s.com
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is = new InputSource("personWithDTD.xml");
        doc = builder.parse(is);

        write(doc);
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    builderFactory.setValidating(true);

    DocumentBuilder builder = null;
    builder = builderFactory.newDocumentBuilder();
    builder.setErrorHandler(new MainClass());
    Document xmlDoc = null;/*  w w  w  .  j av  a 2s  .c o  m*/
    xmlDoc = builder.parse(new File("y.xml"));
    listNodes(xmlDoc);
}

From source file:DOMEdit.java

static public void main(String[] arg) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);
    dbf.setNamespaceAware(true);/* www .j  a v  a2 s .  c o m*/
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is = new InputSource("personWithDTD.xml");
        doc = builder.parse(is);

        findByID(doc, "B");

        write(doc);
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setValidating(true); // and validating parser feaures
    builderFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = null;
    try {/*from w ww .jav a  2 s  .  c  om*/
        builder = builderFactory.newDocumentBuilder(); // Create the parser
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Document xmlDoc = null;

    try {
        xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

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

    } catch (IOException e) {
        e.printStackTrace();
    }
    DocumentType doctype = xmlDoc.getDoctype();
    if (doctype == null) {
        System.out.println("DOCTYPE is null");
    } else {
        System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
    }

    System.out.println("\nDocument body contents are:");
    listNodes(xmlDoc.getDocumentElement(), ""); // Root element & children
}

From source file:TryDOM.java

public static void main(String args[]) throws Exception {

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        builderFactory.setValidating(true);
        builderFactory.setIgnoringElementContentWhitespace(true);

        DocumentBuilder builder = null;
        builder = builderFactory.newDocumentBuilder();
        builder.setErrorHandler(new TryDOM());
        Document xmlDoc = builder.parse(new File("y.xml"));
        DocumentType doctype = xmlDoc.getDoctype();
        System.out.println("DOCTYPE node:\n" + getDoctypeString(doctype));
        listNodes(xmlDoc.getDocumentElement(), "");
    }/*from  w w w . java 2  s  . c o m*/