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

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

public static void main(String[] args) throws Exception {
    // Create the JAXBContext
    JAXBContext jc = JAXBContext.newInstance(Main.class);

    // Create the Object
    Main foo = new Main();
    foo.setBar("Hello World");

    // Create the Document
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.newDocument();

    // Marshal the Object to a Document
    Marshaller marshaller = jc.createMarshaller();
    marshaller.marshal(foo, document);/*from   w  ww  .ja v a  2 s. co m*/

    // Output the Document
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(System.out);
    t.transform(source, result);
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    String initial = "<root><param value=\"abc\"/><param value=\"bc\"/></root>";
    ByteArrayInputStream is = new ByteArrayInputStream(initial.getBytes());
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(is);//from   w  w  w. j av  a  2s . c  o m

    // Create the new xml fragment
    Text a = doc.createTextNode("asdf");
    Node p = doc.createElement("parameterDesc");
    p.appendChild(a);
    Node i = doc.createElement("insert");
    i.appendChild(p);
    Element r = doc.getDocumentElement();
    r.insertBefore(i, r.getFirstChild());
    r.normalize();

    // Format the xml for output
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    // initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);

    System.out.println(result.getWriter().toString());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String s = "<p>" + "  <media type='audio' id='au008093' rights='aaa'>" + "    <title>title</title>"
            + "  this is a test." + "</p>";
    InputStream is = new ByteArrayInputStream(s.getBytes());

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document d = db.parse(is);//w ww.ja v  a 2s  .c o m

    Node rootElement = d.getDocumentElement();
    System.out.println(nodeToString(rootElement));
}

From source file:DOMEdit.java

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

        findByID(doc, "B");

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

From source file:Main.java

public static void main(String[] argv) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Templates template = factory.newTemplates(new StreamSource(new FileInputStream("xsl.xlt")));
    Transformer xformer = template.newTransformer();
    Source source = new StreamSource(new FileInputStream("in.xml"));
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.newDocument();
    Result result = new DOMResult(doc);
    xformer.transform(source, result);//from  w ww . j  a  va  2  s.c  o m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputSource source = new InputSource(new StringReader("<root>\n" + "<field name='firstname'>\n"
            + "    <value>John</value>\n" + "</field>\n" + "<field name='lastname'>\n"
            + "    <value>Citizen</value>\n" + "</field>\n" + "<field name='DoB'>\n"
            + "    <value>01/01/1980</value>\n" + "</field>\n" + "<field name='Profession'>\n"
            + "    <value>Manager</value>\n" + "</field>\n" + "</root>"));

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    Document document = documentBuilder.parse(source);

    NodeList allFields = (NodeList) document.getElementsByTagName("field");

    Map<String, String> data = new HashMap<>();
    for (int i = 0; i < allFields.getLength(); i++) {
        Element field = (Element) allFields.item(i);
        String nameAttribute = field.getAttribute("name");
        Element child = (Element) field.getElementsByTagName("value").item(0);
        String value = child.getTextContent();
        data.put(nameAttribute, value);/*from   www.j a  v a 2  s . com*/
    }

    for (Map.Entry field : data.entrySet()) {
        System.out.println(field.getKey() + ": " + field.getValue());
    }
}

From source file:DOMEdit.java

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

            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);//from  w w w .j av  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);

            importName(doc, 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 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);

            modifyingTextbyCuttingandPasting(doc);

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