Java XML Child Element Add addElementChildTo(Element parent, String nsURI, String qualifiedName)

Here you can find the source of addElementChildTo(Element parent, String nsURI, String qualifiedName)

Description

Adds an Element child to the given element.

License

Open Source License

Parameter

Parameter Description
parent The Element to add the Element child to.
qualifiedName The fully qualified name of the new Element child.

Return

Returns the new child.

Declaration

public static Element addElementChildTo(Element parent, String nsURI, String qualifiedName) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.*;

public class Main {
    /**/* ww  w . ja  v  a2s .  com*/
     * Adds an {@link Element} child to the given element.
     * <p>
     * If you only want to generate elements for a document that is to be
     * serialized, this method is fine.  If, however, you want to manipulate
     * the document, you should probably use
     * {@link #addElementChildTo(Element,String,String)}.
     *
     * @param parent The {@link Element} to add the {@link Element} child to.
     * @param tagName The tag name of the new {@link Element} child.
     * @return Returns A new {@link Element} with its <code>nodeName</code> set
     * to <code>tagName</code>, and <code>localName</code>, <code>prefix</code>,
     * and <code>namespaceURI</code> set to <code>null</code>.
     * @see #addElementChildTo(Element,String,String)
     */
    public static Element addElementChildTo(Element parent, String tagName) {
        final Document doc = parent.getOwnerDocument();
        final Element child = doc.createElement(tagName);
        parent.appendChild(child);
        return child;
    }

    /**
     * Adds an {@link Element} child to the given element.
     *
     * @param parent The {@link Element} to add the {@link Element} child to.
     * @param qualifiedName The fully qualified name of the new {@link Element}
     * child.
     * @return Returns the new {@link Element} child.
     */
    public static Element addElementChildTo(Element parent, String nsURI, String qualifiedName) {
        final Document doc = parent.getOwnerDocument();
        final Element child = doc.createElementNS(nsURI, qualifiedName);
        parent.appendChild(child);
        return child;
    }
}

Related

  1. addChildElements(Node n, List elements)
  2. addChildNode(Node pNode, String name, String value)
  3. addChildren(Element element, List children)
  4. addElement(Element parent, Element child)
  5. addElement(Element parent, Element childElement)
  6. appendElement(Element parent, Element child)
  7. appendElement(Element parent, Element child)
  8. appendTextNode(Node parent, String child)