Java Utililty Methods XML Node Append

List of utility methods to do XML Node Append

Description

The list of methods to do XML Node Append are organized into topic(s).

Method

CommentaddComment(Node node, String comment)
add Comment
Document doc = null;
if (node.getNodeType() == Node.DOCUMENT_NODE) {
    doc = (Document) node;
} else {
    doc = node.getOwnerDocument();
Comment e = doc.createComment(comment);
node.appendChild(e);
...
voidaddDescendants(Node node, List descendants)
Adds descendant elements of a node to a list.
NodeList children = node.getChildNodes();
int numChildren = children.getLength();
for (int i = 0; i < numChildren; i++) {
    Node child = children.item(i);
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        descendants.add(child);
        addDescendants(child, descendants);
voidaddImageSource(Node test, List holder)
add Image Source
if (test.getNodeType() != Node.ELEMENT_NODE)
    return;
Element RealTest = (Element) test;
Node SourceNode = RealTest.getAttributeNode("SRC");
if (SourceNode == null)
    return;
String text = SourceNode.getNodeValue();
if (text == null || text.length() == 0)
...
voidaddNodeValue(Node node, String name, boolean value)
add Node Value
addNodeValue(node, name, value ? "true" : "false");
NodeaddValue(Node node, String value)
Add a value to the specified node
final Node newNode;
if (node instanceof Document)
    newNode = ((Document) node).createTextNode(value);
else
    newNode = node.getOwnerDocument().createTextNode(value);
if (newNode != null)
    node.appendChild(newNode);
return newNode;
...
voidappendChild(Node parent, Node child)
Checks if child element has same owner document before appending to the parent, and imports it to the parent's document if necessary.
Document ownerDoc = getOwnerDocument(parent);
if (child.getOwnerDocument() != ownerDoc) {
    parent.appendChild(ownerDoc.importNode(child, true));
} else {
    parent.appendChild(child);
NodeappendForeignChild(Node node, Node foreignChild)
append Foreign Child
return node.appendChild(node.getOwnerDocument().importNode(foreignChild, true));
booleancanAppend(Node node, Node parentNode)
Checks if the node can be appended on the given parent node
if (node == null || parentNode == null || node == parentNode || isAncestorOf(node, parentNode)) {
    return false;
return true;