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

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    addText(doc.getDocumentElement(), "new text");

    System.out.println(documentToString(doc));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    printAttributes(doc.getDocumentElement());

    System.out.println(documentToString(doc));

}

From source file:Main.java

public static void main(String... args) throws IOException, SAXException, ParserConfigurationException {

    String xml = "<T rn='0'>"
            + "<I><P>UNKNOWN</P><K>UNKNOWN</K><N><O>UNKNOWN</O><P>UNKNOWN</P><U>UNKNOWN</U><N>UNKNOWN</N></N></I></T>";

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));

    print(doc.getDocumentElement(), "");
}

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    dom(doc.getDocumentElement());//w ww  . ja v  a  2 s  .  c o m

    System.out.println(documentToString(doc));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    System.out.println(getAttributeValue(doc.getDocumentElement(), "attr"));

    System.out.println(documentToString(doc));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    System.out.println(hasAttribute(doc.getDocumentElement(), "value"));

    System.out.println(documentToString(doc));

}

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    replacePerson(doc, "newName", "111111", "newEmail@email.com");

    System.out.println(documentToString(doc));

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader("<root>\r\n" + //
            "<Users>\r\n" + //
            "   <App id=\"test\">\r\n" + //
            "      <Username>ADMIN</Username>\r\n" + //
            "      <Password>ADMIN</Password>\r\n" + //
            "   </App>\r\n" + //
            "</Users>\r\n" + //
            "<Users>\r\n" + //
            "   <App id=\"test2\">\r\n" + //
            "      <Username>ADMIN2</Username>\r\n" + //
            "      <Password>ADMIN2</Password>\r\n" + //
            "   </App>\r\n" + //
            "</Users>\r\n" + //
            "</root>")));
    String inputId = "test2";
    String xpathStr = "//Users/App[@id='" + inputId + "']";
    // retrieve elements and change their content
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xpathStr + "/Username");
    Node username = (Node) expr.evaluate(doc, XPathConstants.NODE);
    username.setTextContent("test-username");
    expr = xpath.compile(xpathStr + "/Password");
    Node password = (Node) expr.evaluate(doc, XPathConstants.NODE);
    password.setTextContent("test-password");
    // output the document
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    System.out.println(writer.toString());

}

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 .  ja v a2 s. co m*/

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    addComment(doc);/*from   ww  w  .java  2  s  .  co  m*/

    System.out.println(documentToString(doc));

}