Java XML Element Child Append appendNewElement(Document document, Element parent, String element, Object content, String namespace)

Here you can find the source of appendNewElement(Document document, Element parent, String element, Object content, String namespace)

Description

append New Element

License

LGPL

Declaration

public static Element appendNewElement(Document document,
            Element parent, String element, Object content, String namespace) 

Method Source Code

//package com.java2s;
/*/*from  w w w  .  j a v a  2s. c o  m*/
 * Copyright (C) 2013 4th Line GmbH, Switzerland
 *
 * The contents of this file are subject to the terms of either the GNU
 * Lesser General Public License Version 2 or later ("LGPL") or the
 * Common Development and Distribution License Version 1 or later
 * ("CDDL") (collectively, the "License"). You may not use this file
 * except in compliance with the License. See LICENSE.txt for more
 * information.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

import org.w3c.dom.*;

public class Main {
    public static Element appendNewElement(Document document,
            Element parent, Enum el) {
        return appendNewElement(document, parent, el.toString());
    }

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

    public static Element appendNewElement(Document document,
            Element parent, String element, Object content) {
        return appendNewElement(document, parent, element, content, null);
    }

    public static Element appendNewElement(Document document,
            Element parent, String element, Object content, String namespace) {
        Element childElement;
        if (namespace != null) {
            childElement = document.createElementNS(namespace, element);
        } else {
            childElement = document.createElement(element);
        }

        if (content != null) {
            // TODO: We'll have that on Android 2.2:
            // childElement.setTextContent(content.toString());
            // Meanwhile:
            childElement.appendChild(document.createTextNode(content
                    .toString()));
        }

        parent.appendChild(childElement);
        return childElement;
    }
}

Related

  1. appendChildElement(final Element parentElement, final String name, final String value)
  2. appendChildElementBoolean(Element parentNode, String tagName, boolean content)
  3. appendChildElmt(Element aElmt, String aChildName)
  4. appendNewComment(Element parentElement, String commentText)
  5. appendNewElement(Document document, Element parent, Enum el)
  6. appendNewElement(Node parent, String elementName)
  7. appendNewElementIfNotNull(Document document, Element parent, Enum el, Object content, String namespace)
  8. appendNewElementIfNotNull(Document document, Element parent, Enum el, Object content, String namespace)
  9. appendNode(Node node, StringBuilder sb, int indent)