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

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

Description

get Int Attribute Required

License

Apache License

Declaration

public static int getIntAttributeRequired(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 int getIntAttributeRequired(Node node, String attributeName) throws Exception {
        String string = getStringAttributeRequired(node, attributeName);
        try {/*w ww .j  a v  a2  s.c  o  m*/
            return Integer.parseInt(string);
        } catch (Exception e) {
            throw new Exception("Could not read integer from value '" + string + "' in attribute '" + attributeName
                    + "' in node '" + node.getLocalName() + "'");
        }
    }

    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() + "'");
        }
        String text = value.getTextContent();
        if (text == null) {
            throw new Exception("Missing text  '" + attributeName + "' in node '" + node.getNodeName() + "'");
        }

        return text;
    }
}

Related

  1. getIntAttr(Node node, String attrName)
  2. getIntAttribute(Node n, String s)
  3. getIntAttribute(Node node, String attr)
  4. getIntAttribute(Node node, String attributeName, int defaultValue)
  5. getIntAttribute(Node node, String name, int defVal)
  6. getIntAttributeValue(Node node, String attribute)
  7. getIntAttrId(Node aNode, String aAttrName)
  8. getIntegerAttribute(Node n, String attributeName)
  9. getIntegerAttribute(Node node, String att_name)