Java tutorial
//package com.java2s; //License from project: Apache License import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; public class Main { private static final String RESOURCE_ITEM = "item"; private static final String ATTRIBUTE_TYPE = "type"; public static void setResourceAttributeValue(Node node, String resourceType, String attribute, String value) { String currentNodeName = node.getNodeName(); if (currentNodeName.startsWith(resourceType)) { setAttributeValue(node, attribute, value); } else if (currentNodeName.startsWith(RESOURCE_ITEM)) { String typeValue = getAttributeValue(node, ATTRIBUTE_TYPE); if (resourceType.equals(typeValue)) { setAttributeValue(node, attribute, value); } } } public static void setAttributeValue(Node node, String attribute, String value) { NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { Node nameAttr = attributes.getNamedItem(attribute); if (nameAttr != null) { nameAttr.setNodeValue(value); } } } public static String getAttributeValue(Node node, String attribute) { NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { Node nameAttr = attributes.getNamedItem(attribute); if (nameAttr != null) { return nameAttr.getNodeValue(); } } return null; } }