Java XML Attribute Add addOrUpdateAttribute(Element element, String name, String value)

Here you can find the source of addOrUpdateAttribute(Element element, String name, String value)

Description

Update an attribute for an Element with the provided value.

License

Open Source License

Parameter

Parameter Description
element a parameter
name a parameter
value a parameter

Declaration

public static void addOrUpdateAttribute(Element element, String name, String value) 

Method Source Code

//package com.java2s;
/*!/* w  w w .  jav  a 2s  . c  o  m*/
 * HITACHI VANTARA PROPRIETARY AND CONFIDENTIAL
 *
 * Copyright 2002 - 2017 Hitachi Vantara. All rights reserved.
 *
 * NOTICE: All information including source code contained herein is, and
 * remains the sole property of Hitachi Vantara and its licensors. The intellectual
 * and technical concepts contained herein are proprietary and confidential
 * to, and are trade secrets of Hitachi Vantara and may be covered by U.S. and foreign
 * patents, or patents in process, and are protected by trade secret and
 * copyright laws. The receipt or possession of this source code and/or related
 * information does not convey or imply any rights to reproduce, disclose or
 * distribute its contents, or to manufacture, use, or sell anything that it
 * may describe, in whole or in part. Any reproduction, modification, distribution,
 * or public display of this information without the express written authorization
 * from Hitachi Vantara is strictly prohibited and in violation of applicable laws and
 * international treaties. Access to the source code contained herein is strictly
 * prohibited to anyone except those individuals and entities who have executed
 * confidentiality and non-disclosure agreements or other agreements with Hitachi Vantara,
 * explicitly covering such access.
 */

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

public class Main {
    /**
     * Update an attribute for an Element with the provided value. If
     * it does not exist, add it.
     *
     * @param element
     * @param name
     * @param value
     */
    public static void addOrUpdateAttribute(Element element, String name, String value) {
        NamedNodeMap measureAttrs = element.getAttributes();
        Node attrNode = measureAttrs.getNamedItem(name);
        if (attrNode != null) {
            attrNode.setNodeValue(value);
        } else {
            element.setAttribute(name, value);
        }
    }
}

Related

  1. addDoubleAttributeAsInteger(Element element, String attName, double value)
  2. addElement(Node parent, String name, Attr[] attrs)
  3. addElementAndAttributes(Node parent, String name, String[] atts)
  4. addNewElementWithAttribute(Node node, String elementName, String elementValue, String attrName, String attrValue)
  5. addOptionalPropertyReference(BeanDefinitionBuilder builder, String propertyName, Attr attribute, String defaultValue)
  6. addPropertyReferenceIfNeeded(BeanDefinitionBuilder bdb, Element element, String attributeName)
  7. addRequiredPropertyValue(BeanDefinitionBuilder builder, String propertyName, Element element, String attributeName)
  8. addTextElement(Node parent, String name, String value, Attr[] attrs)
  9. addTypeAttribute(Element element, String type)