Java XML Attribute Get getAttributeValues(Node node, String nodeName, String attrName)

Here you can find the source of getAttributeValues(Node node, String nodeName, String attrName)

Description

Get an array of specific attributes for specific elements in a Node tree.

License

Open Source License

Parameter

Parameter Description
node the top of the tree to search.
nodeName the element names to include in the search.
attrName the name of the attributes to include.

Return

the array of attribute values natching the criteria.

Declaration

public static String[] getAttributeValues(Node node, String nodeName, String attrName) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import java.util.ArrayList;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**/*w  ww . j  a  va 2s.  c o  m*/
     * Get an array of specific attributes for specific elements
     * in a Node tree. If the node is a Document, use the document
     * element as the starting point.
     * @param node the top of the tree to search.
     * @param nodeName the element names to include in the search.
     * @param attrName the name of the attributes to include.
     * @return the array of attribute values natching the criteria.
     */
    public static String[] getAttributeValues(Node node, String nodeName, String attrName) {
        if (node instanceof Document)
            node = ((Document) node).getDocumentElement();
        if (!(node instanceof Element))
            return new String[0];
        NodeList nodeList = ((Element) node).getElementsByTagName(nodeName);
        ArrayList list = new ArrayList();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Attr attr = ((Element) nodeList.item(i)).getAttributeNode(attrName);
            if (attr != null)
                list.add(attr.getValue());
        }
        String[] values = new String[list.size()];
        return (String[]) list.toArray(values);
    }
}

Related

  1. getAttributeValueNS(@Nonnull final Element aElement, @Nullable final String sNamespaceURI, @Nonnull final String sAttrName, @Nullable final String sDefault)
  2. getAttributeValueNS(Element element, String namespace, String attrName)
  3. getAttributeValueOrNull(NamedNodeMap attributes, String attributeName)
  4. getAttributeValuePair(Node node)
  5. getAttributeValues(Element el)
  6. getAttributeWithInheritance(Element element, String attributeName)
  7. getAttributName(Node node)
  8. getAttributValue(Node n, String name)
  9. getAttrInt(Element root, String attrName)