Example usage for org.w3c.dom Element getAttribute

List of usage examples for org.w3c.dom Element getAttribute

Introduction

In this page you can find the example usage for org.w3c.dom Element getAttribute.

Prototype

public String getAttribute(String name);

Source Link

Document

Retrieves an attribute value by name.

Usage

From source file:Main.java

/**
 * Trys to find a child element in the given parent element where the child's
 * name attribute is the given value.//from w  ww  .  j  a v a  2s .c  o  m
 *
 * @param parent The Element to search in.
 * @param name   The name attribute of the element to search for.
 *
 * @return The Element if found, null otherwise.
 */
public static Element findElementWithNameAttribute(Element parent, String name) {
    NodeList l = parent.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n.getNodeType() == n.ELEMENT_NODE) {
            Element e = (Element) n;
            if (e.getAttribute("name").equals(name)) {
                return e;
            }
        }
    }
    return null;
}

From source file:Main.java

public static String getStringAttr(Element element, String name, String def) {
    String attr = element.getAttribute(name);
    if (attr.isEmpty()) {
        attr = def;//from ww w. ja va 2 s .  co m
    }
    return attr;
}

From source file:Main.java

public static Element getChildWithAttributeValue(Element parent, String attrName, String attrValue) {
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node instanceof Element) {
            Element element = (Element) node;
            if (attrValue.equals(element.getAttribute(attrName))) {
                return element;
            }//  w w  w  .  j  a v  a  2 s. c  o  m
        }
    }
    return null;
}

From source file:Main.java

public static HashMap<String, Element> buildIndex(Element element, String tagName, String idName) {
    NodeList nl = element.getElementsByTagName(tagName);
    HashMap<String, Element> index = new HashMap<>(nl.getLength());
    for (int i = 0; i < nl.getLength(); i++) {
        Element el = (Element) nl.item(i);
        String id = el.getAttribute(idName);
        if (!id.isEmpty())
            index.put(id, el);/*from   w ww  .jav  a2 s .com*/
    }
    return index;
}

From source file:Main.java

public static boolean isAttributeTrue(Element elem, String attribute) {
    return elem.hasAttribute(attribute) && "true".equals(elem.getAttribute(attribute));
}

From source file:Main.java

public static boolean readBoolAttr(Element element, String attributeName) {
    String attributeValue = element.getAttribute(attributeName);
    return Boolean.parseBoolean(attributeValue);
}

From source file:Main.java

/**
 * Get child elements by classname/*  w  w w .j a  v  a2 s .  c  o  m*/
 *
 * @param ele parent element
 * @param cname of elements to find
 */
public static List<Element> getElementsByClass(Element ele, String cname) {
    Vector<Element> list = new Vector();
    NodeList nl = ele.getChildNodes();
    for (int j = 0; j < nl.getLength(); j++) {
        if (nl.item(j).getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element e = (Element) nl.item(j);
        if (e.getAttribute("class").equals(cname))
            list.add(e);
    }
    return (list);
}

From source file:Main.java

/**
 * returns the first element with the given tag which attribute "name" equals name string.<br>
 * search only in Element child nodes/*from w w w .j a v  a  2 s  . c o m*/
 * @param tagName   the tag to search
 * @param name      the "name" attribute value
 * @param searchIn   the element, in which child Nodes will be searched
 * @return   the first element found that matches
 * @throws Exception
 */
public static Element getChildElementWithName(String tagName, String name, Element searchIn) {
    String attribute = "name";
    ArrayList<Element> list = getChildElementsByTag(tagName, searchIn);
    for (Element e : list) {
        if (name.equals(e.getAttribute(attribute))) {
            return e;
        }
    }
    return null;
}

From source file:Main.java

public static Integer getIntegerAttr(Element element, String name, int def) {
    String attr = element.getAttribute(name);
    if (!attr.isEmpty()) {
        Integer ret = Integer.valueOf(attr);
        return ret;
    }//from  ww  w .ja  v a  2  s .c  o  m
    return def;
}

From source file:Main.java

/**
 * Get a boolean attribute from the supplied element.
 * @param element The element.//  www . j a  v a2  s . co m
 * @param attribName The attribute name.
 * @return True if the attribute value is "true" (case insensitive), otherwise false.
 */
public static boolean getBooleanAttrib(Element element, String attribName) {
    String attribVal = element.getAttribute(attribName);
    return (attribVal != null && attribVal.equalsIgnoreCase("true"));
}