Java XML Child Element Create createChild(Node node, String elemName)

Here you can find the source of createChild(Node node, String elemName)

Description

Creates a new Node-object with the specified name and appends it as a child element to the provided Node- parameter.

License

Open Source License

Parameter

Parameter Description
elem the <code>Node</code> to which the newly created <code>Node</code> will be appended to. Not allowed to be <code>null</code>.
elemName the name of the to-be-created <code>Element</code>, not allowed to be empty or <code>null</code>.

Return

the created element

Declaration

public static Element createChild(Node node, String elemName) 

Method Source Code

//package com.java2s;
/*/*from  w  ww  .j ava 2 s  .com*/
 * This file is part of ROOSSTER.
 * Copyright 2004, Benjamin Reitzammer <benjamin@roosster.org>
 * All rights reserved.
 *
 * ROOSSTER is free software; you can redistribute it and/or modify
 * it under the terms of the Artistic License.
 *
 * You should have received a copy of the Artistic License
 * along with ROOSSTER; if not, go to
 * http://www.opensource.org/licenses/artistic-license.php for details
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**
     * Creates a new <code>Node</code>-object with the specified <code>name</code>
     * and appends it as a child element to the provided <code>Node</code>-
     * parameter.
     * 
     * @param elem the <code>Node</code> to which the newly created 
     * <code>Node</code> will be appended to. Not allowed to be <code>null</code>.
     * @param elemName the name of the to-be-created <code>Element</code>, not allowed 
     * to be empty or <code>null</code>.
     * @return the created element
     * @exception IllegalArgumentException if <code>elem</code> and/ord
     * <code>elemName</code> are null (or empty in the case of <code>elemName</code>)
     */
    public static Element createChild(Node node, String elemName) {
        if (node == null || elemName == null || "".equals(elemName))
            throw new IllegalArgumentException("Arguments are not allowed to be null or empty");

        Document document = null;

        if (node instanceof Document) {
            document = (Document) node;
        } else if (node.getOwnerDocument() != null) {
            document = node.getOwnerDocument();
        }

        Element newChild = null;
        if (document != null) {
            newChild = document.createElement(elemName);
            node.appendChild(newChild);
        }

        return newChild;
    }
}

Related

  1. appendDateNode(Document owner, Element appendElement, String name, Date date)
  2. createAndAppendChild(Element parentElement, String elementName, Properties attributes)
  3. createChild(Document document, Node parent, String childNodeName)
  4. createChild(Element el, String name, boolean attach)
  5. createChild(final Element el, final String name)
  6. createChildElement(Document doc, Element parent, String name)
  7. createChildElement(Document doc, Node node, String nodeName)
  8. createChildElement(Element parent, String name)
  9. createChildElement(Element parentElement, String elementName)