Example usage for org.w3c.dom Element appendChild

List of usage examples for org.w3c.dom Element appendChild

Introduction

In this page you can find the example usage for org.w3c.dom Element appendChild.

Prototype

public Node appendChild(Node newChild) throws DOMException;

Source Link

Document

Adds the node newChild to the end of the list of children of this node.

Usage

From source file:Main.java

/**
 * Creates an element without text content
 *///from  w  ww  .  jav  a  2  s .c o  m
public static Element createElement(Element parent, Document document, String name) {
    Element element = document.createElement(name);
    parent.appendChild(element);
    return element;
}

From source file:Main.java

/**
 * Append text value node.//from ww w . j a  v a2 s  .  c o  m
 *
 * @param baseNode
 *            the base node
 * @param tagName
 *            the tag name
 * @param tagValue
 *            the tag value
 * @return the element
 */
public static Element appendTextValueNode(Node baseNode, String tagName, String tagValue) {
    Element newNode = appendNode(baseNode, tagName);
    if (tagValue == null) {
        newNode.appendChild(baseNode.getOwnerDocument().createTextNode("<null>"));
    } else {
        newNode.appendChild(baseNode.getOwnerDocument().createTextNode(tagValue));
    }
    return newNode;
}

From source file:Main.java

public static Element createChildElement(Element parentElement, String strTagName) {
    Element eleChildElement = parentElement.getOwnerDocument().createElement(strTagName);
    parentElement.appendChild(eleChildElement);
    return eleChildElement;
}

From source file:Main.java

public static void setText(Element ele, String stext) {
    try {/*from  w w  w  .j  a  v a  2 s .  co  m*/
        if (null == ele)
            return;
        if (null == stext) {
            ele.appendChild(ele.getOwnerDocument().createTextNode(""));
        } else {
            ele.getFirstChild().setNodeValue(stext);
        }
    } catch (Exception e) {
        ele.appendChild(ele.getOwnerDocument().createTextNode(stext));
    }
}

From source file:DOMEdit.java

public static void importName(Document doc1, Document doc2) {
        Element root1 = doc1.getDocumentElement();
        Element personInDoc1 = (Element) root1.getFirstChild();

        Node importedPerson = doc2.importNode(personInDoc1, true);

        Element root2 = doc2.getDocumentElement();
        root2.appendChild(importedPerson);
    }//from www. ja v a  2s.  c  o  m

From source file:Main.java

/**
 * Inserts a new value for an XML tag specified by <code>tagName</code> name
 * in a <code>Document</code> object.
 * /*from  w  w w. ja v a 2 s .  co m*/
 * @param doc
 *            Document object.
 * @param tagName
 *            Name of the tag as String.
 * @param tagValue
 *            Value of the tag as String.
 */
public static Element insertTagValue(Document doc, String tagName, String tagValue) {
    Element element = doc.createElement(tagName);
    doc.getDocumentElement().appendChild(element);
    if (tagValue != null) {
        element.appendChild(doc.createTextNode(tagValue));
    }
    return element;
}

From source file:Main.java

/**
 * Appends a child element to the provided parent element.
 * This child element is created with the name and string value provided.
 *
 * @param parentElement the parent element
 * @param name the name for the child element to be created
 * @param value the string value to be assigned to the created child element
 * @return the newly created child element 
 *//*w  w w .ja va2 s. c  om*/
public static Element appendChildElement(final Element parentElement, final String name, final String value) {
    Document parentDocument = parentElement.getOwnerDocument();
    Element createElement = createElement(parentDocument, name);
    if (value != null) {
        createElement.appendChild(parentDocument.createTextNode(value));
    }
    parentElement.appendChild(createElement);
    return createElement;
}

From source file:Main.java

public static Element appendNewElement(Document document, Element parent, String element) {
    Element child = document.createElement(element);
    parent.appendChild(child);
    return child;
}

From source file:Main.java

static public Element addChildElement(Element parentElement, String elementName) {
    Element childElement = parentElement.getOwnerDocument().createElement(elementName);
    parentElement.appendChild(childElement);
    return childElement;
}

From source file:Main.java

/**
 * Creates an element with the specified tag and appends it to the element supplied
 * @param element - the element to add the created element to
 * @param tag - the tag name for the element to create
 * @return The element created//from  www  .j av  a2  s.co  m
 */
public static Element appendElement(Element element, String tag) {
    Document document = element.getOwnerDocument();
    Element childElement = document.createElement(tag);
    element.appendChild(childElement);
    return childElement;
}