Example usage for org.w3c.dom Document createElement

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

Introduction

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

Prototype

public Element createElement(String tagName) throws DOMException;

Source Link

Document

Creates an element of the type specified.

Usage

From source file:Main.java

public static Element addChildElement(Element element, String name, String text) {
    Document document = element.getOwnerDocument();
    Element result = (Element) element.appendChild(document.createElement(name));
    if (text != null)
        result.appendChild(document.createTextNode(text));

    return result;
}

From source file:Main.java

public static void nodeSubInsert(Element root, String table, String queryType, String queryValue) {
    Node find = getTag(root, table);
    Document doc = find.getOwnerDocument();

    Element type = doc.createElement(queryType);

    Text value = doc.createTextNode(queryValue);

    type.appendChild(value);//ww  w .ja v  a2  s . c  om
    find.appendChild(type);

    print(doc);

}

From source file:Main.java

public static void sendXml(String result, int operationN) throws Exception {
    String filepath = "file.xml";
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(filepath);

    Node tonode = doc.getElementsByTagName("command").item(0);
    Element res = doc.createElement("result");
    res.setTextContent(result);/*from   w  w w  .  j  av  a 2s.  com*/
    tonode.appendChild(res);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result1 = new StreamResult(new File("file2.xml"));

    StreamResult result3 = new StreamResult(System.out);
    transformer.transform(source, result1);
}

From source file:Main.java

/**
 * //from   w w  w .j a  v  a2s .c om
 */
public static Document createVmlDocument() {
    Document document = createDocument();

    Element root = document.createElement("html");
    root.setAttribute("xmlns:v", "urn:schemas-microsoft-com:vml");
    root.setAttribute("xmlns:o", "urn:schemas-microsoft-com:office:office");

    document.appendChild(root);

    Element head = document.createElement("head");

    Element style = document.createElement("style");
    style.setAttribute("type", "text/css");
    style.appendChild(document.createTextNode("<!-- v\\:* {behavior: url(#default#VML);} -->"));

    head.appendChild(style);
    root.appendChild(head);

    Element body = document.createElement("body");
    root.appendChild(body);

    return document;
}

From source file:Utils.java

/**
 * Add a new element to the given parent
 * @param parent the parent Element//from   w  w w .  jav  a 2  s .  co m
 * @param name the child name
 * @return new Element
 */
public static Element createElement(Element parent, String name) {
    Document document;
    Element element;

    document = parent.getOwnerDocument();
    element = document.createElement(name);

    parent.appendChild(element);
    return element;
}

From source file:Main.java

/**
 * Add the first node with a namespace attribute as below:
 * <asset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 * @nodeName: name of the element//  w  w  w  . ja va  2s  . c  om
 * @
 * */
public static Document addNodeWithNSAttribute(Document document, String nodeName, String attrName,
        String namespace) {
    Element node = document.createElement(nodeName);
    Attr attr = document.createAttribute(attrName);
    attr.setValue(namespace);
    node.setAttributeNodeNS(attr);
    document.appendChild(node);
    return document;
}

From source file:Main.java

public static void createChildTextWithComment(Document doc, Element elem, String name, String value,
        String comment) {/*from   w  ww  .j  ava2 s. com*/
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(value == null ? "" : value));
    Comment c = doc.createComment(comment);
    elem.appendChild(c);
    elem.appendChild(child);

}

From source file:Main.java

public static Document createXMLResult(String rootName, Map<String, String> map, Document d) {

    try {// w  w  w  . ja  v  a2  s  .com

        Element r = d.createElement(rootName);
        d.appendChild(r);

        for (String elementName : map.keySet()) {
            Element eltName = d.createElement(elementName);
            eltName.appendChild(d.createTextNode(map.get(elementName)));
            r.appendChild(eltName);
        }

        d.normalizeDocument();

        return d;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

private static void insertNode(String nodeName, String nodeValue) {
    File f = new File(System.getProperty("user.dir") + "\\DBConf.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;/* w w  w .jav  a  2 s . co  m*/
    try {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(f);
        Element tempElem = doc.createElement(nodeName);
        Text child = doc.createTextNode(nodeValue);
        tempElem.appendChild(child);
        doc.getElementsByTagName("conf").item(0).appendChild(tempElem);
        DOMSource source = new DOMSource(doc);
        StreamResult Streamres = new StreamResult(new FileOutputStream(f));
        TransformerFactory.newInstance().newTransformer().transform(source, Streamres);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Element createRootElement(Document rootDoc, String rootTagName) {
    if (rootDoc.getDocumentElement() == null) {
        Element root = rootDoc.createElement(rootTagName);
        rootDoc.appendChild(root);/*from w w  w  . j ava 2 s  .  co  m*/
        return root;
    }
    return rootDoc.getDocumentElement();
}