Example usage for org.w3c.dom Document createAttribute

List of usage examples for org.w3c.dom Document createAttribute

Introduction

In this page you can find the example usage for org.w3c.dom Document createAttribute.

Prototype

public Attr createAttribute(String name) throws DOMException;

Source Link

Document

Creates an Attr of the given name.

Usage

From source file:Main.java

public static void addAttributeToElement(Document doc, Element projectElement, String attributeName,
        String attributeValue) {//from   w w w.  j  a va 2 s.com
    Attr xmlnsAttr = doc.createAttribute(attributeName);
    xmlnsAttr.setValue(attributeValue);
    projectElement.setAttributeNode(xmlnsAttr);
}

From source file:Main.java

public static void addAttribute(Document document, Node node, String name, String value) {
    if (value != null) {
        Attr attribute = document.createAttribute(name);
        attribute.setValue(value);//w  ww . j  av a  2s  .c  om
        node.getAttributes().setNamedItem(attribute);
    }
}

From source file:Main.java

/**
 * Adds an attribute to the specified document element.
 * @param doc the document./*ww w  . j  a v a 2  s. c o m*/
 * @param parent the parent element.
 * @param name the attribute name.
 * @param value the attribute value.
 * @return the attribute.
 */
public static Attr addAttribute(final Document doc, final Element parent, final String name,
        final String value) {
    Attr attr = doc.createAttribute(name);
    attr.setNodeValue(value);
    parent.setAttributeNode(attr);
    return attr;
}

From source file:Main.java

/**
 * Utility method to add an attribute to a given element
 *
 * @param document/*  w  ww. j  a v  a 2s  .c  o m*/
 * @param AttribName
 * @param attribValue
 * @param element
 */
public static void addAttribute(Document document, String AttribName, String attribValue, Element element) {
    Attr attribute = document.createAttribute(AttribName);
    attribute.setValue(attribValue);
    element.setAttributeNode(attribute);
}

From source file:Main.java

public static Attr addAttribut(Document doc, String tag, String value) {
    Attr attr = doc.createAttribute(tag);
    attr.setValue(value);//  ww w.j  a  v  a2 s . c  om
    return attr;
}

From source file:Main.java

/**
 * Add the first node with a namespace attribute as below:
 * <asset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 * @nodeName: name of the element//  w w w .  j  a v  a 2s  .  c o  m
 * @
 * */
public static Document addNodeWithNSAttribute(Document document, String nodeName, String attrName,
        String namespace) {
    Element node = document.createElement(nodeName);
    Attr attr = document.createAttribute(attrName);
    attr.setValue(namespace);
    node.setAttributeNodeNS(attr);
    document.appendChild(node);
    return document;
}

From source file:Main.java

public static void setNodeAttribute(Document doc, Node n, String attrName, String value) {
    Node namedAttr = n.getAttributes().getNamedItem(attrName);
    if (namedAttr == null) {
        namedAttr = doc.createAttribute(attrName);
        n.getAttributes().setNamedItem(namedAttr);
    }/*from   ww w .  jav a 2s  .  c o m*/
    namedAttr.setNodeValue(value);
}

From source file:Main.java

/**
 * Add an attribute to an Element./*  www.j a  v  a2 s. co  m*/
 * @param doc XML document
 * @param element XML Element
 * @param attribute attribute name
 * @param value attribute value
 */
public static void addAttribute(final Document doc, final Element element, final String attribute,
        final String value) {

    final NamedNodeMap atts = element.getAttributes();
    final Attr newAttr = doc.createAttribute(attribute);
    newAttr.setValue(value);
    atts.setNamedItem(newAttr);
}

From source file:Main.java

public static Attr createAttr(String attrName, String attrValue, Document document) {
    if (attrName == null || attrName.length() == 0 || attrValue == null || document == null) {
        return null;
    }//from  ww w  .j  a va  2 s. c  o  m

    Attr attribute = document.createAttribute(attrName);
    attribute.setValue(attrValue);
    return attribute;
}

From source file:Main.java

public static Element newElementWithAttrs(String tagName, String... attrNames) {
    Document document = newDocument();
    Element element = newElement(document, tagName);

    for (String attr : attrNames) {
        element.appendChild(document.createAttribute(attr));
    }/*from   w  w w .  java  2s. c o  m*/

    return element;
}