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

booleangetElementBooleanValue(Element element, String attribute)
get Element Boolean Value
return getElementBooleanValue(element, attribute, false);
booleangetElementBooleanValue(Element element, String attribute, boolean defaultValue)
get Element Boolean Value
if (!element.hasAttribute(attribute))
    return defaultValue;
return Boolean.valueOf(getElementStringValue(element, attribute));
ElementgetElementByAttr(NodeList elements, String attrName)
get Element By Attr
Element element = null;
for (int i = 0; i < elements.getLength(); i++) {
    element = (Element) elements.item(i);
    if (element.hasAttribute(attrName))
        break;
return element;
String[]getElements(Element root, String tagName, String attrName)
get Elements
NodeList list = root.getElementsByTagName(tagName);
String[] result = new String[list.getLength()];
for (int i = 0; i < list.getLength(); i++) {
    Element el = (Element) list.item(i);
    result[i] = (attrName == null) ? el.getNodeName() : el.getAttribute(attrName);
return (result);
ListgetElementsByTagAndAttr(Element parent, String elemName, String attrName, String attrVal)
get Elements By Tag And Attr
List<Element> result = new ArrayList<Element>();
NodeList nl = parent.getElementsByTagName(elemName);
for (int i = 0; i < nl.getLength(); i++) {
    if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
        Element elem = (Element) nl.item(i);
        String val = elem.getAttribute(attrName);
        if (val != null && val.equals(attrVal))
            result.add(elem);
...
StringgetElementStringValue(Element element, String attribute)
get Element String Value
return element.getAttribute(attribute);
StringgetElementStringValue(Element element, String attribute)
get Element String Value
return element.getAttribute(attribute);
StringgetElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue)
Retrieves text of element by attribute value (e.g.
String str = "";
NodeList list = modsroot.getElementsByTagName(nodename);
for (int i = 0; i < list.getLength(); i++) {
    Node node = (Node) list.item(i);
    NamedNodeMap attrs = node.getAttributes();
    Attr attr = (Attr) attrs.getNamedItem(attrname);
    if (attr != null) {
        if (attr.getValue().equals(attrvalue)) {
...
CollectiongetElementValues(final String elementName, final String attributeValue, final InputStream is)
get Element Values
final Collection<String> elementValues = new ArrayList<>();
final XMLEventReader xmlEventReader = XMLInputFactory.newInstance()
        .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
final StringBuffer characters = new StringBuffer();
boolean read = false;
while (xmlEventReader.peek() != null) {
    final XMLEvent event = (XMLEvent) xmlEventReader.next();
    switch (event.getEventType()) {
...
StringgetFirstAttribute(Element elem, String name, String attrName)
Get the attribute value of a given attribute name for the first XML org.w3c.dom.Element of given name.
NodeList nodeList = elem.getElementsByTagName(name);
if (nodeList.getLength() == 0) {
    return null;
return (((Element) nodeList.item(0)).getAttribute(attrName));