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

/**
 * The method creates an element with specified name and attributes and appends it to the parent element.
 * This is a convenience method for often used sequence of calls.
 *
 * @param parent - the node where to append the new element.
 * @param name - the name of the element type being created.
 * @param attributes - string array of pairs attributes and their values.
 *     Each attribute must have a value, so the array must have even number of elements.
 * @return the newly created element.//  w  w  w . j  av  a 2s .c  o  m
 *
 * @throws ArrayIndexOutOfBoundsException in case of odd number of elements of the attribute array
 *    (i.e. the last attribute is missing a value).
 */
public static Element appendElement(Node parent, String name, String[] attributes) {
    Document doc = parent instanceof Document ? (Document) parent : parent.getOwnerDocument();
    Element element = doc.createElement(name);
    if (attributes != null) {
        int attrLen = attributes.length;
        for (int i = 0; i < attrLen; i += 2) {
            String attrName = attributes[i];
            String attrValue = attributes[i + 1];
            element.setAttribute(attrName, attrValue);
        }
    }
    parent.appendChild(element);
    return element;
}

From source file:Main.java

/**
 * Rename an element, replacing it in its document.
 * @param element the element to rename.
 * @param name the new element name./*ww  w  . j  a  v  a  2  s . c om*/
 * @return the renamed element.
 */
public static Element renameElement(Element element, String name) {
    if (element.getNodeName().equals(name))
        return element;
    Element el = element.getOwnerDocument().createElement(name);

    //Copy the attributes
    NamedNodeMap attributes = element.getAttributes();
    int nAttrs = attributes.getLength();
    for (int i = 0; i < nAttrs; i++) {
        Node attr = attributes.item(i);
        el.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

    //Copy the children
    Node node = element.getFirstChild();
    while (node != null) {
        Node clone = node.cloneNode(true);
        el.appendChild(clone);
        node = node.getNextSibling();
    }

    //Replace the element
    element.getParentNode().replaceChild(el, element);
    return el;
}

From source file:com.nortal.jroad.util.SOAPUtil.java

public static void addArrayAnyTypeAttribute(Element element, int size) {
    element.setAttribute("SOAP-ENC:arrayType", getAnyTypeAttribute(size));
}

From source file:com.nortal.jroad.util.SOAPUtil.java

public static void addArrayOffsetAttribute(Element element, int offset) {
    element.setAttribute("SOAP-ENC:offset", new StringBuilder("[").append(offset).append("]").toString());
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void setAttrIntValue(Element anElement, String aName, int aValue) {
    Integer intObject;//from  w  w w .j  a  va 2s .c o m

    intObject = aValue;
    anElement.setAttribute(aName, intObject.toString());
}

From source file:com.nortal.jroad.util.SOAPUtil.java

public static void addArrayTypeAttribute(Element element, String type, int size) {
    element.setAttribute("SOAP-ENC:arrayType", getArrayTypeAttribute(type, size));
}

From source file:com.microsoft.tfs.core.clients.versioncontrol.workspacecache.WorkItemCheckedInfo.java

/**
 * Save a list of work items checked info as XML.
 *
 * @param parentNode/*from  ww w .  j  av  a  2  s.co m*/
 *        XML parent node
 * @param list
 *        checked info list
 */
public static void saveAsXML(final Element parentNode, final WorkItemCheckedInfo[] list) {
    // XML document format:
    // <Parent Node>
    // <WorkItemsCheckedInfo>
    // <Item ID="45" Checked="1">
    // <Item ID="99" Checked="0">
    // </WorkItemsCheckedInfo>
    // </Parent Node>

    // Sanity check and also an easy case for empty list.
    if (list == null || list.length == 0) {
        return;
    }

    // Create the work item list node.
    final Element listRoot = DOMUtils.appendChild(parentNode, XML_TAG_WORK_ITEM_CHECKED_INFO);

    // Loop and insert the work items checked info.
    for (final WorkItemCheckedInfo checkedInfo : list) {
        final Element itemNode = DOMUtils.appendChild(listRoot, XML_TAG_WORK_ITEM);

        itemNode.setAttribute(XML_TAG_ATTRID, XMLConvert.toString(checkedInfo.getID()));
        itemNode.setAttribute(XML_TAG_ATTR_CHECKED, checkedInfo.isChecked() ? "1" : "0"); //$NON-NLS-1$//$NON-NLS-2$
        itemNode.setAttribute(XML_TAG_ATTR_ACTION, actionToString(checkedInfo.getCheckinAction()));
    }
}

From source file:DOMEdit.java

public static void dupAttributes(Document doc) {
        Element root = doc.getDocumentElement();
        Element personOne = (Element) root.getFirstChild();

        Attr deptAttr = personOne.getAttributeNode("dept");
        personOne.removeAttributeNode(deptAttr);
        String deptString = deptAttr.getValue();
        personOne.setAttribute("dept", deptString + " updated");

        String mailString = personOne.getAttribute("mail");
        personOne.setAttribute("mail", mailString + " updated");

        String titleString = personOne.getAttribute("title");
        //personOne.removeAttribute("title");
        personOne.setAttribute("title", titleString + " updated");
    }// ww  w  .  ja va  2 s .  c  o  m

From source file:de.akra.idocit.wsdl.services.DocumentationGenerator.java

/**
 * Creates a documentation {@link Element} out of <code>docparts</code>. It sets the
 * attributes and adds the addressee elements.
 * //from w  w w .j  a  va  2s.  c o m
 * Please note: if the parameter <code>thematicGridName</code> is <code>null</code>,
 * no thematicgrid-element will be added to the documentation-element. In this case
 * only the docparts will appear.
 * 
 * @param tagName
 *            Qualified tag name for the documentation element.
 * @param docparts
 *            The documentation parts that should be written into a documentation
 *            element.
 * @return The generated documentation {@link Element} or <code>null</code> if
 *         <code>docparts != null && !docparts.isEmpty()</code>.
 */
public static Element generateDocumentationElement(String tagName, List<Documentation> docparts,
        String thematicGridName) {
    Element docElem = null;
    if (docparts != null && !docparts.isEmpty()) {
        try {
            if (domDocument == null) {
                // must be initialized here, because of error handling
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder;
                builder = factory.newDocumentBuilder();
                domDocument = builder.newDocument();
            }
            docElem = domDocument.createElement(tagName);

            if (thematicGridName != null) {
                // Create Thematic Grid Element
                Element thematicGrid = domDocument.createElement(DocumentationParser.THEMATIC_GRID_TAG_NAME);
                thematicGrid.setAttribute(DocumentationParser.THEMATIC_GRID_ATTRIBUTE_NAME, thematicGridName);
                docElem.appendChild(thematicGrid);

                for (Documentation doc : docparts) {
                    thematicGrid.appendChild(generateDocpartElement(doc));
                }
            } else {
                for (Documentation doc : docparts) {
                    docElem.appendChild(generateDocpartElement(doc));
                }
            }
        } catch (ParserConfigurationException e) {
            logger.log(Level.SEVERE, "This error should not occur.", e);
        }
    }

    return docElem;
}

From source file:Utils.java

public static Element findContainerWithAttributeValueElseCreate(Document document, Element parent,
        String element, String attributeName, String attributeValue) {

    NodeList nl = parent.getElementsByTagName(element);
    Element e;
    for (int i = 0; i < nl.getLength(); i++) {
        e = (Element) nl.item(i);
        if (e.getAttribute(attributeName).equals(attributeValue)) {
            return e;
        }//from ww  w .  j  a  v  a2  s .  c om
    }

    e = document.createElement(element);
    parent.appendChild(e);
    e.setAttribute(attributeName, attributeValue);

    return e;
}