Example usage for org.w3c.dom Element setAttribute

List of usage examples for org.w3c.dom Element setAttribute

Introduction

In this page you can find the example usage for org.w3c.dom Element setAttribute.

Prototype

public void setAttribute(String name, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:Main.java

/**
 * Set name of specified generic element
 *///  w  ww  .j  av a 2 s.  c  o  m
public static void setGenericElementName(Element element, String name) {
    if (element != null)
        element.setAttribute(ATTR_NAME_NAME, name);
}

From source file:Main.java

/**
 *
 * @param parent Element/* w w w  .  j a va 2  s.  c  o m*/
 * @param name String
 * @param value String
 */
public static void appendAttributeNode(Element parent, String name, String value) {
    parent.setAttribute(name, value);
}

From source file:Main.java

/**
 * create an element from a document, that has some text content in it
 * @param doc - document/* ww w . ja  va  2  s  . c o  m*/
 * @param name - element name
 * @param attribute - attribute
 * @param value - value
 * @return element object
 */
public static Element createElement(Document doc, String name, String attribute, String value) {
    Element e = doc.createElement(name);
    e.setAttribute(attribute, value);
    return e;
}

From source file:Main.java

public static void setIntegerAttribute(Element node, String attr, int value) {
    lastState = DONE;//from w  w w .j ava2 s . c  o m
    try {
        node.setAttribute(attr, String.valueOf(value));
    } catch (NumberFormatException ex) {
        lastState = ERROR;
    }
}

From source file:Main.java

public static Element addElement(Element parent, String childName, String attributeName, String attributeValue,
        Document doc) {/*  w ww.j  a v a  2 s  . c o  m*/
    Element childElement = doc.createElementNS(DEFAULT_NAMESPACE, childName);
    childElement.setAttribute(attributeName, attributeValue);
    parent.appendChild(childElement);
    return childElement;
}

From source file:Main.java

/**
 * Add XML IDs to a Document. This method is one of the hearts of the xseq
 * system. It runs through a DOM Document tree, and for each element found
 * adds (updates) an ID attribute called "id".
 * /*from w w w.  ja  va 2  s  . co  m*/
 * <P>
 * For now the value is a simple sequence.
 * 
 * @param doc
 *            The DOM Document to which IDs are added
 */
public static void addIDs(Document doc) {
    // * means all elements
    NodeList nodelist = doc.getElementsByTagName("*");

    // does this need to be thread protected?

    for (int i = 0; i < nodelist.getLength(); i++) {
        Element element = (Element) nodelist.item(i);

        element.setAttribute("id", "n" + i);
    }
}

From source file:Main.java

public static Element addTemplateNode(Document document, Element parent, String name) {
    Element element = document.createElement(TEMPLATE_NODE);
    parent.appendChild(element);/*from  w ww  .ja  va  2 s.c  o m*/
    element.setAttribute(NAME_ATTR, name);
    return element;
}

From source file:Main.java

/**
 * Helper method for creating slot key-value pairs in the GnuCash XML structure.
 * <p>This method is only a helper for creating slots whose values are of string type</p>
 * @param doc {@link org.w3c.dom.Document} for creating nodes
 * @param key Slot key as string/* www. ja  v  a  2 s.  com*/
 * @param value Slot value as String
 * @return Element node containing the key-value pair
 */
public static Element createSlot(Document doc, String key, String value, String valueType) {
    Element slotNode = doc.createElement(TAG_SLOT);
    Element slotKeyNode = doc.createElement(TAG_SLOT_KEY);
    slotKeyNode.appendChild(doc.createTextNode(key));
    Element slotValueNode = doc.createElement(TAG_SLOT_VALUE);
    slotValueNode.setAttribute(ATTR_KEY_TYPE, valueType);
    slotValueNode.appendChild(doc.createTextNode(value));
    slotNode.appendChild(slotKeyNode);
    slotNode.appendChild(slotValueNode);

    return slotNode;
}

From source file:Main.java

private static Element addPropertyNode(Document document, Element parent, String name) {
    Element element = document.createElement(PROPERTY_NODE);
    parent.appendChild(element);/*from  ww w  .j  a v  a 2  s .  c o m*/
    element.setAttribute(NAME_ATTR, name);
    return element;
}

From source file:Main.java

/**
 * Gets the arg element/*w  ww .  j a v a 2s  .com*/
 * @param text the text content
 * @param xml the Document to use to create the element
 * @return the arg element
 */
public static Element getArg(String text, Document xml, boolean value) {
    Element arg = xml.createElement("arg");
    if (value)
        arg.setAttribute("value", text);
    else
        arg.setTextContent(text);
    return arg;
}