Java XML Child Element Create createChildElement(Document doc, Node node, String nodeName)

Here you can find the source of createChildElement(Document doc, Node node, String nodeName)

Description

Create a child of the given node.

License

Open Source License

Parameter

Parameter Description
doc a document
node a node
nodeName a node name

Return

org.w3c.dom.Element

Declaration

public static Element createChildElement(Document doc, Node node, String nodeName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2003, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://  w ww. j av  a2 s  .  c om
 *     IBM Corporation - Initial API and implementation
 *******************************************************************************/

import org.w3c.dom.*;

public class Main {
    /**
     * Create a child of the given node at the given index.
     *
     * @param doc a document
     * @param element an element
     * @param index an index
     * @param nodeName a node name
     * @return org.w3c.dom.Element
     */
    public static Element createChildElement(Document doc, Element element, int index, String nodeName) {
        Element element2 = doc.createElement(nodeName);
        try {
            NodeList childList = element.getElementsByTagName(nodeName);
            Node child = childList.item(index);
            element.insertBefore(element2, child);
        } catch (Exception e) {
            element.appendChild(element2);
        }
        return element2;
    }

    /**
     * Create a child of the given node.
     *
     * @param doc a document
     * @param node a node
     * @param nodeName a node name
     * @return org.w3c.dom.Element
     */
    public static Element createChildElement(Document doc, Node node, String nodeName) {
        Element element = doc.createElement(nodeName);
        node.appendChild(element);
        return element;
    }
}

Related

  1. createChild(Document document, Node parent, String childNodeName)
  2. createChild(Element el, String name, boolean attach)
  3. createChild(final Element el, final String name)
  4. createChild(Node node, String elemName)
  5. createChildElement(Document doc, Element parent, String name)
  6. createChildElement(Element parent, String name)
  7. createChildElement(Element parentElement, String elementName)
  8. createChildElement(Element parentElement, String strTagName)
  9. createChildElement(final String tagName, final Node parent, final Document document)