Java XML Attribute Get getAttributeValue(Node sNode, String attribName)

Here you can find the source of getAttributeValue(Node sNode, String attribName)

Description

Utility method to fetch the attribute value from the given element node

License

Open Source License

Parameter

Parameter Description
sNode a parameter
attribName a parameter

Declaration

public static String getAttributeValue(Node sNode, String attribName) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**//from   w w  w . j  a  va2s  .  co  m
     *
     * @param token
     *            XPath token fragment
     * @param attrib
     *            attribute whose value must be fetched
     * @return
     */
    private static String getAttributeValue(String token, String attrib) {
        int attribStart = token.indexOf(attrib);
        int valStart = token.indexOf('=', attribStart) + 1/* skip past the separtor */;
        int valEnd = token.indexOf('@', valStart);
        if (valEnd < 0) {
            valEnd = valStart + (token.length() - valStart - 1);
        } else {
            valEnd = token.length() - 1 /* valEnd should be index */;
        }
        String value = token.substring(valStart, valEnd);
        int s = value.indexOf('\'');
        if (s > -1) {
            s++;
            int e = value.lastIndexOf('\'');
            return value.substring(s, e);
        } else {
            // also handle ""
            s = value.indexOf('"');
            if (s > -1) {
                s++;
                int e = value.lastIndexOf('"');
                return value.substring(s, e);
            }
        }
        return value;
    }

    /**
     * Utility method to fetch the attribute value from the given
     * element node
     *
     * @param sNode
     * @param attribName
     * @return
     */
    public static String getAttributeValue(Node sNode, String attribName) {
        String value = null;
        NamedNodeMap attrs = sNode.getAttributes();
        if (attrs != null) {
            Node attr = attrs.getNamedItem(attribName);
            if (attr != null) {
                value = attr.getNodeValue();
            }
        }
        return value;
    }

    /**
     * Utility method to fetch the value of the element node
     *
     * @param node
     * @return
     */
    public static String getNodeValue(Node node) {
        for (Node child = node.getFirstChild(); child != null; child = child
                .getNextSibling()) {
            if (child.getNodeType() == Node.TEXT_NODE) {
                return child.getNodeValue();
            }
        }
        return null;
    }
}

Related

  1. getAttributeValue(Node node, String name)
  2. getAttributeValue(Node node, String name)
  3. getAttributeValue(Node node, String name)
  4. getAttributeValue(Node node, String name, boolean defaultValue)
  5. getAttributeValue(Node node, String name, String defaultValue)
  6. getAttributeValue(Node sNode, String attribName)
  7. getAttributeValue(NodeList elements, String attributeName)
  8. getAttributeValue(StartElement element, String namespaceURI, String localPart)
  9. getAttributeValue(StartElement startElement, String attributeName)