Example usage for org.w3c.dom Node appendChild

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

Introduction

In this page you can find the example usage for org.w3c.dom Node 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

public static void createNewCDATA(Node elem, String value) {
    Document doc = elem.getOwnerDocument();
    if (value == null) {
        value = "";
    }/*  ww  w  .  j av a2s  .c om*/
    CDATASection cdata = doc.createCDATASection(value);
    elem.appendChild(cdata);
}

From source file:Main.java

/**
 *
 * @param doc Document/*from  w  w w . j av a  2  s.  co m*/
 * @param parent Node
 * @param name String
 * @param value String
 * @return Element
 */
public static Element appendChildNode(Document doc, Node parent, String name, String value) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(value));
    parent.appendChild(child);
    return child;
}

From source file:Main.java

/**
 * This method is used to insert a new tag below the tag specified by
 * <code>appendTo</code> parameter.
 * //from w w w  . ja v  a 2 s .  co  m
 * @param d
 *            the <code>Document</code> object to which a new tag is to be
 *            inserted.
 * @param appendTo
 *            the tag below which a new tag needs to be inserted.
 * @param tagName
 *            the name of new tag
 * @param tagValue
 *            the value of new tag
 */
public static Element insertNewTagBelow(Document d, String appendTo, String tagName, String tagValue) {
    Node element = d.getElementsByTagName(appendTo).item(0);
    if (element == null) {
        element = d.createElement(appendTo);
    }
    Element newElement = d.createElement(tagName);
    element.appendChild(newElement);
    newElement.appendChild(d.createTextNode(tagValue));
    return newElement;
}

From source file:Main.java

public static void appendCollection(Node node, Object[] o, String tag) {
    Element child = node.getOwnerDocument().createElement(tag);
    for (int i = 0; i < o.length; i++) {
        child.setAttribute("element_" + i, (String) o[i]);
    }//  ww  w .  jav  a 2s.c o m
    node.appendChild(child);
}

From source file:Main.java

public static final void addNodeValue(Node node, String name, String value) {
    if (value != null) {
        Document doc = node.getOwnerDocument();
        Element e = doc.createElement(name);
        e.appendChild(doc.createTextNode(value));
        node.appendChild(e);
    }/*from  www .j a va2  s. c o m*/
}

From source file:Main.java

public static final void addNodeCDATAValue(Node node, String name, String value) {
    if (value != null) {
        Document doc = node.getOwnerDocument();
        Element e = doc.createElement(name);
        e.appendChild(doc.createCDATASection(value));
        node.appendChild(e);
    }//from   w w  w . j  a  v a  2 s  .  c om
}

From source file:Main.java

/**
   Return the first children of a node with a given name. If no
   child has that name a new one of type element is added to the
   node and returned. /* w w w. j  a v  a2 s. co m*/
 */
static public Node findChildAssertive(Node node, String child_name) {
    if (node == null)
        return null;

    Node child = findChild(node, child_name);
    if (child != null)
        return child;
    return node.appendChild(node.getOwnerDocument().createElement(child_name));
}

From source file:Main.java

/**
 *
 * @param doc Document/*www .  j  av a  2s.  c om*/
 * @param parent Node
 * @param name String
 * @param value int
 * @return Element
 */
public static Element appendChildNode(Document doc, Node parent, String name, int value) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(new Integer(value).toString()));
    parent.appendChild(child);
    return child;
}

From source file:Main.java

public static void moveDown(Node currentN) {
    Node nextSibling = findNextElement(currentN, false);
    Node nextNextSibling = findNextElement(nextSibling, false);
    Node parent = currentN.getParentNode();
    parent.removeChild(currentN);//ww w. jav  a2  s .  co  m
    if (nextNextSibling != null) {
        parent.insertBefore(currentN, nextNextSibling);
    } else
        parent.appendChild(currentN);

}

From source file:Main.java

/**
 * This is a helper function to deal with problems that occur when importing Nodes from
 * JTidy Documents to Xerces Documents./*  w  w  w.  ja  v  a  2s.  com*/
 */
public static Node importNode(Document d, Node n, boolean deep) {
    Node r = cloneNode(d, n);
    if (deep) {
        NodeList nl = n.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node n1 = importNode(d, nl.item(i), deep);
            r.appendChild(n1);
        }
    }
    return r;
}