Java XML Attribute from Node getStringAttributeRequired(Node node, String attributeName)

Here you can find the source of getStringAttributeRequired(Node node, String attributeName)

Description

get String Attribute Required

License

Apache License

Declaration

public static String getStringAttributeRequired(Node node, String attributeName) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static String getStringAttributeRequired(Node node, String attributeName) throws Exception {
        NamedNodeMap attributes = node.getAttributes();
        Node value = attributes.getNamedItem(attributeName);
        if (value == null) {
            throw new Exception("Missing attribute '" + attributeName + "' in node '" + node.getNodeName() + "'");
        }/*  w  w w.ja  va  2 s  . co m*/
        String text = value.getTextContent();
        if (text == null) {
            throw new Exception("Missing text  '" + attributeName + "' in node '" + node.getNodeName() + "'");
        }

        return text;
    }
}

Related

  1. getNodeAttributeValueNS(Node node, String namespaceURI, String attrName)
  2. getNodeMap(NamedNodeMap artifactAttributes)
  3. getNodesByAttributeValue(Node node, String attrName, String attrValue)
  4. getNonEmptyAttribute(Element element, String namespace, String localName)
  5. getStringAttributeOptional(Node node, String attributeName, String valueIfEmpty)
  6. getValueForAttribute(Node currentNode, String attributeName)
  7. getXMLDate(Element e, String attrName)
  8. getXMLDate(Element e, String attrName)
  9. getXMLInt(Element e, String attrName)