Example usage for org.w3c.dom Document createTextNode

List of usage examples for org.w3c.dom Document createTextNode

Introduction

In this page you can find the example usage for org.w3c.dom Document createTextNode.

Prototype

public Text createTextNode(String data);

Source Link

Document

Creates a Text node given the specified string.

Usage

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser
    Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    Text text = xmlDoc.createTextNode("text");

}

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

    // Create the new xml fragment
    Text a = doc.createTextNode("asdf");
    Node p = doc.createElement("parameterDesc");
    p.appendChild(a);//from ww  w  .  j  ava  2s  .  c o m
    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[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from w w  w . j  a v a2  s .  c o  m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Element element = doc.getDocumentElement();
    Text text = doc.createTextNode("data\n");
    element.appendChild(text);

    text = doc.createTextNode("<>&\"'");
    element.appendChild(text);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
    dFactory.setValidating(false);//w  w  w. j a va2  s  .c om
    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:MainClass.java

public static void main(String args[]) throws IOException {
    Document dom = DOMImplementation.createDocument(null, null, null);

    Element root = dom.createElement("A");

    Element child1 = dom.createElement("B");

    child1.appendChild(dom.createTextNode("C"));

    child1.setAttribute("A", "a");

    root.appendChild(child1);//w w w . ja v a2  s .  c  o  m

    dom.appendChild(root);

    XMLSerializer serial = new XMLSerializer(System.out, null);
    serial.serialize(dom.getDocumentElement());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from w  ww .  j ava  2 s.  c  o m*/
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    element = doc.createElement("root");
    doc.appendChild(element);

    element.appendChild(doc.createTextNode("D"));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);// w  ww.ja  v a 2 s  .  co m
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    element = doc.createElement("root");
    doc.appendChild(element);

    element.insertBefore(doc.createTextNode("A"), element.getFirstChild());

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/* www .  ja  v  a  2 s.  com*/
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    element = doc.createElement("root");
    doc.appendChild(element);

    element.insertBefore(doc.createTextNode("C"), element.getLastChild());

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from   w w w  . ja  v  a2s .c  om
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    element = doc.createElement("root");
    doc.appendChild(element);

    element.getParentNode().insertBefore(doc.createTextNode("B"), element);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from   ww  w  .j a v a2  s  .  co m*/
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();

    Element root = doc.createElementNS(null, "person"); // Create Root Element
    Element item = doc.createElementNS(null, "name"); // Create element
    item.appendChild(doc.createTextNode("Jeff"));
    root.appendChild(item); // Attach element to Root element
    item = doc.createElementNS(null, "age"); // Create another Element
    item.appendChild(doc.createTextNode("28"));
    root.appendChild(item); // Attach Element to previous element down tree
    item = doc.createElementNS(null, "height");
    item.appendChild(doc.createTextNode("1.80"));
    root.appendChild(item); // Attach another Element - grandaugther
    doc.appendChild(root); // Add Root to Document

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS");

    LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer
                                                       // for the DOM
    LSOutput out = domImplLS.createLSOutput();
    StringWriter stringOut = new StringWriter(); // Writer will be a String
    out.setCharacterStream(stringOut);
    ser.write(doc, out); // Serialize the DOM

    System.out.println("STRXML = " + stringOut.toString()); // DOM as a String
}