Here you can find the source of getLongAttributeByName(Node node, String name, long defaultValue)
Parameter | Description |
---|---|
node | the node to get the attribute from |
name | the name of the attribute |
defaultValue | the value to return if the node did not contain the attribute |
public static long getLongAttributeByName(Node node, String name, long defaultValue)
//package com.java2s; //License from project: LGPL import org.w3c.dom.*; public class Main { /**//w w w .j a v a 2 s.c om * Returns the long attribute value for the passed name in the passed node. * @param node the node to get the attribute from * @param name the name of the attribute * @param defaultValue the value to return if the node did not contain the attribute * @return The attribute value or the default value if it is not found. */ public static long getLongAttributeByName(Node node, String name, long defaultValue) { String s = getAttributeByName(node, name, null); if (s == null) return defaultValue; try { return Long.parseLong(s.trim()); } catch (Exception e) { return defaultValue; } } /** * Returns the attribute value for the passed name in the passed node. * @param node the node to get the attribute from * @param name the name of the attribute * @param defaultValue the value to return if the node did not contain the attribute * @return The attribute value or the default value if it is not found. */ public static String getAttributeByName(Node node, String name, String defaultValue) { try { String val = getAttributeValueByName(node, name); if (val != null && !val.trim().isEmpty()) return val.trim(); } catch (Exception e) { } return defaultValue; } /** * Returns the value of a named node attribute in the form of a boolean * @param node The node to retrieve the attribute from * @param name The name of the attribute * @param defaultValue The default value if the attribute cannot be located or converted. * @return true or false */ public static boolean getAttributeByName(Node node, String name, boolean defaultValue) { if (node == null || name == null) return defaultValue; try { return getAttributeBooleanByName(node.getAttributes(), name); } catch (Exception e) { return defaultValue; } } /** * Returns the value of a named node attribute in the form of an int * @param node The node to retrieve the attribute from * @param name The name of the attribute * @param defaultValue The default value if the attribute cannot be located or converted. * @return an int */ public static int getAttributeByName(Node node, String name, int defaultValue) { if (node == null || name == null) return defaultValue; try { return new Double(getAttributeValueByName(node, name).trim()).intValue(); } catch (Exception e) { return defaultValue; } } /** * Returns the value of a named node attribute in the form of a long * @param node The node to retrieve the attribute from * @param name The name of the attribute * @param defaultValue The default value if the attribute cannot be located or converted. * @return a long */ public static long getAttributeByName(Node node, String name, long defaultValue) { if (node == null || name == null) return defaultValue; try { return new Double(getAttributeValueByName(node, name).trim()).longValue(); } catch (Exception e) { return defaultValue; } } /** * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found. * If it is not found, returns a null. * @param nnm NamedNodeMap * @param name String * @return String */ public static String getAttributeValueByName(NamedNodeMap nnm, String name) { for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); if (attr.getName().equalsIgnoreCase(name)) { return attr.getValue(); } } return null; } /** * Returns the attribute value for the passed name in the passed node. * @param node the node to get the attribute from * @param name the name of the attribute * @return The attribute value or null if it is not found. */ public static String getAttributeValueByName(Node node, String name) { return getAttributeValueByName(node.getAttributes(), name); } /** * Searches throgh the passed NamedNodeMap for an attribute. If it is found, it will try to convert it to a boolean. * @param nnm NamedNodeMap * @param name String * @throws RuntimeException on any failure to parse a boolean * @return boolean */ public static boolean getAttributeBooleanByName(NamedNodeMap nnm, String name) throws RuntimeException { for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); if (attr.getName().equalsIgnoreCase(name)) { String tmp = attr.getValue().toLowerCase(); if (tmp.equalsIgnoreCase("true")) return true; if (tmp.equalsIgnoreCase("false")) return false; throw new RuntimeException("Attribute " + name + " value not boolean:" + tmp); } } throw new RuntimeException("Attribute " + name + " not found."); } }