Example usage for javax.xml.parsers DocumentBuilder newDocument

List of usage examples for javax.xml.parsers DocumentBuilder newDocument

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilder newDocument.

Prototype


public abstract Document newDocument();

Source Link

Document

Obtain a new instance of a DOM Document object to build a DOM tree with.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();

    Document document = loader.newDocument();
    Element root = document.createElement("order");
    document.appendChild(root);/*from   ww  w . ja  v a  2  s  .  c o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from w ww  . ja v a  2s. com
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.newDocument();

    Element rootElement = document.createElement("root");
    document.appendChild(rootElement);
    rootElement.setAttributeNS(XMLConstants.XML_NS_URI, "space", "preserve");

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.transform(new DOMSource(document), new StreamResult(System.out));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
    dFactory.setValidating(false);/*from   ww  w. j a v a 2s . co  m*/
    DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
    Document dDoc = dBuilder.newDocument();

    // The root document element. 
    Element pageDataElement = dDoc.createElement("page-data");
    pageDataElement.appendChild(dDoc.createTextNode("Example Text."));

    dDoc.appendChild(pageDataElement);

    System.out.println(dDoc.getDocumentElement().getTextContent());
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/* ww w . j av a2 s .  co m*/

    DocumentBuilder loader = factory.newDocumentBuilder();
    Document document = loader.newDocument();

    String docNS = "http://www.my-company.com";

    Element order = document.createElementNS(docNS, "order");
    document.appendChild(order);
    order.setAttribute("xmlns", docNS);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    DocumentBuilder loader = factory.newDocumentBuilder();

    Document document = loader.newDocument();

    Element order = document.createElement("order");
    document.appendChild(order);/* w  w w .  ja  v a2 s .c o m*/

    XMLSerializer serializer = new XMLSerializer();
    serializer.setOutputCharStream(new java.io.FileWriter("order.xml"));
    serializer.serialize(document);

}

From source file:Main.java

public static void main(String[] args) throws Throwable {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from  w w  w  .  ja v a2s.  c  o  m

    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();

    Element root = doc.createElement("root");
    root.setAttribute("xmlns:m", "http://www.java2s.com/blog");
    root.setAttribute("xmlns:rt", "http://www.java2s.com/forum");
    doc.appendChild(root);

    Element elt = doc.createElement("simple");
    elt.setAttribute("m:Path", "false");
    elt.setAttribute("m:Content", "false");
    elt.setAttribute("rt:file", "false");

    root.appendChild(doc.createTextNode("\n\t"));
    root.appendChild(elt);
    root.appendChild(doc.createTextNode("\n"));
    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc),
            new StreamResult(System.out));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element element = doc.createElement("root");
    doc.appendChild(element);/*from   www .  j  a  v  a  2 s.c o m*/
    Comment comment = doc.createComment("This is a comment");
    doc.insertBefore(comment, element);
    Element itemElement = doc.createElement("item");
    element.appendChild(itemElement);
    itemElement.setAttribute("myattr", "attr>value");
    itemElement.insertBefore(doc.createTextNode("te<xt"), itemElement.getLastChild());
    prettyPrint(doc);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document document = db.newDocument();

    Element book = document.createElement("book");
    book.setAttribute("id", "javanut4");
    document.appendChild(book);/*w ww  .  ja  va 2s.c om*/
    for (int i = 1; i <= 3; i++) {
        Element chapter = document.createElement("chapter");
        Element title = document.createElement("title");
        title.appendChild(document.createTextNode("Chapter " + i));
        chapter.appendChild(title);
        chapter.appendChild(document.createElement("para"));
        book.appendChild(chapter);
    }

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(System.out);

    transformer.transform(source, result);
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);// www .j  a v a2s .  c om

    DocumentBuilder loader = factory.newDocumentBuilder();
    Document document = loader.newDocument();
    edit(document);

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

}

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 w  w.  ja  v a  2 s. c o  m*/
}