Java XML Attribute Add addElement(Node parent, String name, Attr[] attrs)

Here you can find the source of addElement(Node parent, String name, Attr[] attrs)

Description

add Element

License

LGPL

Declaration

public static Element addElement(Node parent, String name, Attr[] attrs) 

Method Source Code

//package com.java2s;
/*//from   ww w.j av  a  2s  .c  o m
 * JFox - The most lightweight Java EE Application Server!
 * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
 *
 * JFox is licenced and re-distributable under GNU LGPL.
 */

import org.w3c.dom.Attr;

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

public class Main {

    public static Element addElement(Node parent, String name, Attr[] attrs) {
        Element element;
        if (parent instanceof Document) {
            element = ((Document) parent).createElement(name);
        } else {
            element = parent.getOwnerDocument().createElement(name);
        }
        if (attrs != null && attrs.length > 0) {
            for (Attr attr : attrs) {
                element.setAttributeNode(attr);
            }
        }
        parent.appendChild(element);
        return element;
    }

    public static Element addElement(Element parent, Element childElement) {
        Node childNode = parent.getOwnerDocument().importNode(childElement, true);
        parent.appendChild(childNode);
        return parent;
    }
}

Related

  1. addAttribute(Node pNode, String attrName, String attrValue)
  2. addAttributes(Element element, String[][] attributes)
  3. addAttributeToXPathExpression(Node nodeAttr, String xpathExpression, String and)
  4. addDomAttr(Document doc, String tagName, String tagNamespaceURI, String attrName, String attrValue)
  5. addDoubleAttributeAsInteger(Element element, String attName, double value)
  6. addElementAndAttributes(Node parent, String name, String[] atts)
  7. addNewElementWithAttribute(Node node, String elementName, String elementValue, String attrName, String attrValue)
  8. addOptionalPropertyReference(BeanDefinitionBuilder builder, String propertyName, Attr attribute, String defaultValue)
  9. addOrUpdateAttribute(Element element, String name, String value)