Java XML Attribute from Node getIntAttrId(Node aNode, String aAttrName)

Here you can find the source of getIntAttrId(Node aNode, String aAttrName)

Description

Looks up the attr and returns the int of it or returns -1

License

Open Source License

Parameter

Parameter Description
aNode the node with the attr

Return

the int or returns -1

Declaration

public static int getIntAttrId(Node aNode, String aAttrName) 

Method Source Code

//package com.java2s;
/*//from ww w  . j  a  va2s . co  m
 * Copyright (c) 2010 The Regents of the University of California.
 * All rights reserved.
 *
 * '$Author: welker $'
 * '$Date: 2010-05-05 22:21:26 -0700 (Wed, 05 May 2010) $' 
 * '$Revision: 24234 $'
 * 
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the above
 * copyright notice and the following two paragraphs appear in all copies
 * of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 * ENHANCEMENTS, OR MODIFICATIONS.
 *
 */

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

public class Main {
    /**
     * Looks up the attr and returns the int of it or returns -1
     * 
     * @param aNode
     *            the node with the attr
     * @return the int or returns -1
     */
    public static int getIntAttrId(Node aNode, String aAttrName) {
        String idStr = findAttrValue(aNode, aAttrName);
        if (idStr != null && idStr.length() > 0) {
            try {
                int id = Integer.parseInt(idStr);
                if (id > -1 && id < Integer.MAX_VALUE) {
                    return id;
                }
            } catch (NumberFormatException e) {
            }
        }
        return -1;
    }

    /**
     * ------------------------------------------------------------ Gets an
     * attribute value for the node.
     * 
     * @param aNode
     *            Parent to search (should be the document root)
     * @param aAttr
     *            Name of attribute to return
     * @return Returns the attribute's value as a string
     *         --------------------------------------------------------------
     */
    public static String findAttrValue(Node aNode, String aAttr) {
        String value = null;
        if (aNode != null) {
            NamedNodeMap map = aNode.getAttributes();
            if (map != null) {
                Node attrNode = map.getNamedItem(aAttr);
                if (attrNode != null) {
                    value = getNodeValue(attrNode);
                }
            }
        }
        return value;
    }

    /**
     * ------------------------------------------------------------ Gets the
     * String value of a node. First checks it's value and if that is null then
     * it checks to see if it has a child node and gets the value of the first
     * child. Assumption: That the first child is a #text node, delibertly NOT
     * checking the first node's type
     * 
     * @param aNode
     *            Parent to search (should be the document root)
     * @return Returns the value of the node
     *         --------------------------------------------------------------
     */
    public static String getNodeValue(Node aNode) {
        String value = null;
        if (aNode.getNodeValue() != null) {
            value = aNode.getNodeValue() != null ? aNode.getNodeValue()
                    .trim() : null;
        } else {
            NodeList list = aNode.getChildNodes();
            if (list.getLength() == 1) {
                Node child = list.item(0);
                if (child != null) {
                    value = child.getNodeValue() != null ? child
                            .getNodeValue().trim() : null;
                }
            }
        }
        return value;
    }
}

Related

  1. getIntAttribute(Node node, String attr)
  2. getIntAttribute(Node node, String attributeName, int defaultValue)
  3. getIntAttribute(Node node, String name, int defVal)
  4. getIntAttributeRequired(Node node, String attributeName)
  5. getIntAttributeValue(Node node, String attribute)
  6. getIntegerAttribute(Node n, String attributeName)
  7. getIntegerAttribute(Node node, String att_name)
  8. getLongAttributeByName(Node node, String name, long defaultValue)
  9. getLowerAttrValue(Node node, String attr)