Java XML Attribute Add addElementAndAttributes(Node parent, String name, String[] atts)

Here you can find the source of addElementAndAttributes(Node parent, String name, String[] atts)

Description

add Element And Attributes

License

Open Source License

Declaration

public static Node addElementAndAttributes(Node parent, String name, String[] atts) 

Method Source Code

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

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 Node addElementAndAttributes(Node parent, String name, String[] atts) {
        Node n = addElement(parent, name);
        for (int i = 0; i < atts.length; i += 2)
            addAttribute(n, atts[i], atts[i + 1]);
        return n;
    }//  ww  w .  j  a v a 2  s  .  c om

    public static Element addElement(Node parent, String name) {
        Element node;
        if (parent.getOwnerDocument() != null)
            node = parent.getOwnerDocument().createElement(name);
        else if (parent instanceof Document)
            node = ((Document) parent).createElement(name);
        else
            return null;
        parent.appendChild(node);
        return node;
    }

    public static Attr addAttribute(Node parent, String name, String value) {
        if (value == null)
            return null;
        Attr node = parent.getOwnerDocument().createAttribute(name);
        try {
            node.setValue(value);
            parent.getAttributes().setNamedItem(node);
        } catch (Exception e) {
            System.out.println("Problem rewriting " + parent.getNodeName() + "." + name + "='" + node.getValue()
                    + "' -> '" + value + "'");
        }
        return node;
    }
}

Related

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