Java Utililty Methods XML Attribute from Node

List of utility methods to do XML Attribute from Node

Description

The list of methods to do XML Attribute from Node are organized into topic(s).

Method

StringgetNodeAttributeValue(Node node, String attributeName)
get Node Attribute Value
return ((Element) node).getAttribute(attributeName);
StringgetNodeAttributeValue(Node node, String attrName)
get Node Attribute Value
NamedNodeMap attrs = node.getAttributes();
if (attrs == null)
    return (null);
Node value = attrs.getNamedItem(attrName);
if (value == null)
    return (null);
return (value.getNodeValue());
StringgetNodeAttributeValueNS(Node node, String namespaceURI, String attrName)
Gets attribute value of a node.
NamedNodeMap attrs = node.getAttributes();
if (attrs == null) {
    return null;
Node value = attrs.getNamedItemNS(namespaceURI, attrName);
if (value == null) {
    return null;
return value.getNodeValue();
MapgetNodeMap(NamedNodeMap artifactAttributes)
get Node Map
Map<String, String> attributeMap = new HashMap<String, String>();
if (artifactAttributes != null) {
    int attributeCount = artifactAttributes.getLength();
    for (int idx = 0; idx < attributeCount; idx++) {
        Node attributeNode = artifactAttributes.item(idx);
        attributeMap.put(attributeNode.getNodeName(), attributeNode.getNodeValue());
return attributeMap;
ListgetNodesByAttributeValue(Node node, String attrName, String attrValue)
Gets a list of nodes that matches the given attribute name and attribute value
List<Node> list = new ArrayList<Node>();
NodeList nodeList = node.getChildNodes();
if (nodeList != null) {
    Node child;
    for (int i = 0; i < nodeList.getLength(); i++) {
        child = nodeList.item(i);
        if (nodeAttributeMatches(child, attrName, attrValue)) {
            list.add(child);
...
StringgetNonEmptyAttribute(Element element, String namespace, String localName)
get Non Empty Attribute
String value = element.getAttributeNS(namespace, localName);
if (value == null || (value = value.trim()).length() == 0 ||
        "???".equals(value)) {
    value = null;
return value;
StringgetStringAttributeOptional(Node node, String attributeName, String valueIfEmpty)
get String Attribute Optional
NamedNodeMap attributes = node.getAttributes();
Node value = attributes.getNamedItem(attributeName);
if (value == null) {
    return valueIfEmpty;
String text = value.getTextContent();
if (text == null) {
    return valueIfEmpty;
...
StringgetStringAttributeRequired(Node node, String attributeName)
get String Attribute Required
NamedNodeMap attributes = node.getAttributes();
Node value = attributes.getNamedItem(attributeName);
if (value == null) {
    throw new Exception("Missing attribute '" + attributeName + "' in node '" + node.getNodeName() + "'");
String text = value.getTextContent();
if (text == null) {
    throw new Exception("Missing text  '" + attributeName + "' in node '" + node.getNodeName() + "'");
...
StringgetValueForAttribute(Node currentNode, String attributeName)
Get the value for the given attribute.
NamedNodeMap attributes = currentNode.getAttributes();
String strCurrentModelName = null;
if (attributes != null) {
    for (int i = 0; i < attributes.getLength(); i++) {
        Node node = attributes.item(i);
        if (node.getNodeName().equalsIgnoreCase(attributeName)) {
            strCurrentModelName = node.getNodeValue();
            break;
...
DategetXMLDate(Element e, String attrName)
get XML Date
String s = e.getAttribute(attrName);
if (s == null || s.length() == 0)
    return null;
try {
    return parseDate(s);
} catch (Exception exc) {
    return null;