Java Utililty Methods XML Attribute from Element

List of utility methods to do XML Attribute from Element

Description

The list of methods to do XML Attribute from Element are organized into topic(s).

Method

ElementgetElement(Element root, String tagName, String attrName, String attrValue)
get Element
if (root.getTagName().equals(tagName)) {
    if (attrName == null) {
        return (root);
    if (root.getAttribute(attrName).equals(attrValue)) {
        return (root);
NodeList list = root.getElementsByTagName(tagName);
for (int i = 0; i < list.getLength(); i++) {
    Element el = (Element) list.item(i);
    if (attrName == null) {
        return (el);
    if (el.getAttribute(attrName).equals(attrValue)) {
        return (el);
return (null);
int[]getElementArrayInt(Element root, String name, String attrib)
Takes a number of tags of name 'name' that are children of 'root', and looks for attributes of 'attrib' on them.
if (root == null)
    return null;
NodeList nl = root.getChildNodes();
LinkedList elementCache = new LinkedList();
int size = nl.getLength();
for (int i = 0; i < size; i++) {
    Node node = nl.item(i);
    if (!(node instanceof Element))
...
String[]getElementArrayString(Element root, String name, String attrib)
Takes a number of tags of name 'name' that are children of 'root', and looks for attributes of 'attrib' on them.
if (root == null)
    return null;
NodeList nl = root.getChildNodes();
LinkedList elementCache = new LinkedList();
int size = nl.getLength();
for (int i = 0; i < size; i++) {
    Node node = nl.item(i);
    if (!(node instanceof Element))
...
StringgetElementAttr(Element element, String attr)
Get the value of the attribute of the element
return element.getAttribute(attr);
StringgetElementAttribute(Element element, String name)
get Element Attribute
String text = element.getAttribute(name);
if (text != null) {
    text = text.trim();
    if (text.length() > 0) {
        return text;
return null;
...
StringgetElementAttribute(Element root, String elementName, String attribute)
get Element Attribute
NodeList list = root.getElementsByTagName(elementName);
if (list.getLength() == 0) {
    return null;
return ((Element) list.item(0)).getAttribute(attribute);
StringgetElementAttribute(Element root, String elemName, String att)
get the value of an Attribute in the Xml Document.
NodeList nl = root.getElementsByTagName(elemName);
if (null == nl) {
    return (null);
Node n = nl.item(0);
if (null == n) {
    return (null);
NamedNodeMap attributes = n.getAttributes();
if (null == attributes) {
    return (null);
n = attributes.getNamedItem(att);
if (null == n) {
    return (null);
return (n.getNodeValue().trim());
StringgetElementAttributes(Element element, List exclude)
get Element Attributes
StringBuffer buffer = new StringBuffer();
if (element != null) {
    NamedNodeMap attributes = element.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            if (!exclude.contains(attr.getNodeName())) {
                buffer.append(attr.getNodeName() + "=");
...
StringgetElementAttributeValue(Element element, String attributeName)
get Element Attribute Value
return element == null ? null : element.getAttribute(attributeName);
booleangetElementBooleanValue(Element element, String attribute)
get Element Boolean Value
return getElementBooleanValue(element, attribute, false);