Create a new XML element in the parent node with the given local name and namespace. - Java XML

Java examples for XML:XML Element Parent

Description

Create a new XML element in the parent node with the given local name and namespace.

Demo Code


//package com.java2s;

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

public class Main {
    /**/*from  w w w  . j a v  a2  s .c  om*/
     * Create a new element in the parent node with the given local name and namespace.
     * @param parent The node to which the new element will be appended.
     * @param namespace The namespace of the new element to be created
     * @param name The local name of the new element to be created
     * @return The newly created element.
     */
    public static Element makeElementNS(Node parent, String namespace,
            String name) {
        Document d = getDoc(parent);
        Element result = d.createElementNS(namespace, name);
        parent.appendChild(result);
        return result;
    }

    private static Document getDoc(Node n) {
        if (n instanceof Document)
            return (Document) n;
        else
            return n.getOwnerDocument();
    }
}

Related Tutorials