Example usage for javax.xml.parsers DocumentBuilderFactory newInstance

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

Introduction

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

Prototype

public static DocumentBuilderFactory newInstance() 

Source Link

Document

Obtain a new instance of a DocumentBuilderFactory .

Usage

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  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);

            editTextbyInsertionandReplacement(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.  jav  a2s . 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);

            addAttribute(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  www  .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);

            delAttribute(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 va 2s . 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);

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

        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);/*www.  j  a v  a  2  s . 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);

            dupAttributes(doc);

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<Services><service name='qwerty' id=''><File rootProfile='abcd' extension='acd'><Columns>"
            + "<name id='0' profileName='DATE' type='java'></name><name id='1' profileName='DATE' type='java'></name>"
            + "</Columns></File><File rootProfile='efg' extension='ghi'><Columns><name id='a' profileName='DATE' type='java'></name>"
            + "<name id='b' profileName='DATE' type='java'></name></Columns></File></service></Services>";
    DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;

    documentBuilder = documentFactory.newDocumentBuilder();

    org.w3c.dom.Document doc = documentBuilder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes())));

    doc.getDocumentElement().normalize();
    NodeList nodeList0 = doc.getElementsByTagName("service");
    NodeList nodeList1 = null;//from w  w  w .  j  a v a  2  s  . com
    NodeList nodeList2 = null;
    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    for (int temp0 = 0; temp0 < nodeList0.getLength(); temp0++) {
        Node node0 = nodeList0.item(temp0);

        System.out.println("\nElement type :" + node0.getNodeName());
        Element Service = (Element) node0;

        if (node0.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        System.out.println("name : " + Service.getAttribute("name"));
        System.out.println("id : " + Service.getAttribute("id"));
        nodeList1 = Service.getChildNodes();
        for (int temp = 0; temp < nodeList1.getLength(); temp++) {
            Node node1 = nodeList1.item(temp);

            System.out.println("\nElement type :" + node1.getNodeName());

            Element File = (Element) node1;

            if (node1.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            System.out.println("rootProfile:" + File.getAttribute("rootProfile"));
            System.out.println("extension  : " + File.getAttribute("extension"));

            nodeList2 = File.getChildNodes();// colums
            for (int temp1 = 0; temp1 < nodeList2.getLength(); temp1++) {
                Element column = (Element) nodeList2.item(temp1);
                NodeList nodeList4 = column.getChildNodes();
                for (int temp3 = 0; temp3 < nodeList4.getLength(); temp3++) {
                    Element name = (Element) nodeList4.item(temp3);
                    if (name.getNodeType() != Node.ELEMENT_NODE) {
                        continue;
                    }
                    System.out.println("id:" + name.getAttribute("id"));
                    System.out.println("profileName  : " + name.getAttribute("profileName"));
                    System.out.println("type  : " + name.getAttribute("type"));
                }
            }
        }
    }
}

From source file:DOMEdit.java

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

            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 .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);

            addProcessingInstruction(doc);

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

From source file:Main.java

public static void main(String argv[]) throws Exception {
    String next = "keyword,123";
    String[] input = next.split(",");

    String textToFind = input[0].replace("'", "\\'"); // "CEO";
    String textToReplace = input[1].replace("'", "\\'"); // "Chief Executive Officer";
    String filepath = "root.xml";
    String fileToBeSaved = "root2.xml";

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(filepath);

    XPath xpath = XPathFactory.newInstance().newXPath();
    // change ELEMENTS

    String xPathExpression = "//*[text()='" + textToFind + "']";
    NodeList nodes = (NodeList) xpath.evaluate(xPathExpression, doc, XPathConstants.NODESET);

    for (int idx = 0; idx < nodes.getLength(); idx++) {
        nodes.item(idx).setTextContent(textToReplace);
    }/*from   w  w w .  j  a v a 2s . co  m*/

    // change ATTRIBUTES
    String xPathExpressionAttr = "//*/@*[.='" + textToFind + "']";
    NodeList nodesAttr = (NodeList) xpath.evaluate(xPathExpressionAttr, doc, XPathConstants.NODESET);

    for (int i = 0; i < nodesAttr.getLength(); i++) {
        nodesAttr.item(i).setTextContent(textToReplace);
    }
    System.out.println("Everything replaced.");

    // save xml file back
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(fileToBeSaved));
    transformer.transform(source, result);
}

From source file:DOMCopy.java

static public void main(String[] arg) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);/*from w  w  w  . java2 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);

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