Example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace

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

Introduction

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

Prototype


public void setIgnoringElementContentWhitespace(boolean whitespace) 

Source Link

Document

Specifies that the parsers created by this factory must eliminate whitespace in element content (sometimes known loosely as 'ignorable whitespace') when parsing XML documents (see XML Rec 2.10).

Usage

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  ww  .  jav a  2s .c o  m

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setValidating(false);//w  w  w  .j a  v  a 2 s  .co m
    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:DOMCopy.java

static public void main(String[] arg) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);/*from w w  w.java2s .  co m*/
    dbf.setNamespaceAware(true);
    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:DOMEdit.java

static public void main(String[] arg) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);/* w  w  w.ja va  2s  . c  o  m*/
    dbf.setNamespaceAware(true);
    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:DOMEdit.java

static public void main(String[] arg) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);/*from   ww w . ja  va 2 s .com*/
    dbf.setNamespaceAware(true);
    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);

        insert(doc, "newName", "1111111111", "newEmail");

        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 {//  ww w  .  j av  a 2s  .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:DOMEdit.java

static public void main(String[] arg) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);/*from w ww  .  ja  v  a 2  s.  c o m*/
        dbf.setNamespaceAware(true);
        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);

            addComment(doc);

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

From source file:DOMEdit.java

static public void main(String[] arg) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);//from ww w  .ja  va  2  s.  c om
        dbf.setNamespaceAware(true);
        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);

            addProcessingInstruction(doc);

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

From source file:DOMEdit.java

static public void main(String[] arg) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);/*from   w  w  w .  j a  v  a  2  s .c  o  m*/
        dbf.setNamespaceAware(true);
        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);

            addCDATA(doc);

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

From source file:DOMEdit.java

static public void main(String[] arg) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);/* w w  w.  j  a va2  s. c  o  m*/
        dbf.setNamespaceAware(true);
        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);

            importName(doc, doc);

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