Java XML Attribute from Element getFirstAttribute(Element elem, String name, String attrName)

Here you can find the source of getFirstAttribute(Element elem, String name, String attrName)

Description

Get the attribute value of a given attribute name for the first XML org.w3c.dom.Element of given name.

License

Apache License

Parameter

Parameter Description
elem the parent XML Element
name the name of the child text Element
attrName the attribute name

Return

attribute value of named child Element

Declaration

public static String getFirstAttribute(Element elem, String name, String attrName) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    /**/*from  w  w w  .  ja  v  a 2  s.c o  m*/
     * Get the attribute value of a given attribute name for
     * the first XML {@code org.w3c.dom.Element} of given name.
     *
     * @param elem the parent XML Element
     * @param name the name of the child text Element
     * @param attrName the attribute name
     * @return attribute value of named child Element
     */
    public static String getFirstAttribute(Element elem, String name, String attrName) {
        NodeList nodeList = elem.getElementsByTagName(name);
        if (nodeList.getLength() == 0) {
            return null;
        }

        return (((Element) nodeList.item(0)).getAttribute(attrName));
    }
}

Related

  1. getElementsByTagAndAttr(Element parent, String elemName, String attrName, String attrVal)
  2. getElementStringValue(Element element, String attribute)
  3. getElementStringValue(Element element, String attribute)
  4. getElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue)
  5. getElementValues(final String elementName, final String attributeValue, final InputStream is)
  6. getFloatAttribute(Element element, String name)
  7. getFloatAttribute(String name, Element el)
  8. getHeadAttr(Element annotU, String attrName)
  9. getIdAttribute(Element domElement)