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

/**
 * Finds and returns the next sibling node with the given name and
 * attribute name, value pair. Since only elements have attributes,
 * the node returned will be of type Node.ELEMENT_NODE.
 *//*w w  w  .ja va  2  s  .  c o  m*/
public static Element getNextSiblingElement(Node node, String elemName, String attrName, String attrValue) {

    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) sibling;
            if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) {
                return element;
            }
        }
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static String fetchSubElementValue(Element parent, String subElementName, String lang) {
    Collection<Element> elements = fetchSubElements(parent, subElementName);

    lang = lang.toUpperCase();// w w  w  . ja v  a  2 s.c  o m

    for (Element e : elements) {
        String lng = e.getAttribute("xml:lang");
        if (lng != null && lang.equals(lng.toUpperCase())) {
            return getTextContent(e);
        }
    }

    return fetchSubElementValue(parent, subElementName);
}

From source file:Main.java

/**
 * Adds a reference for the given object to the references map if it has a refId
 *
 * @author Tristan Bepler//from   w ww . ja va 2 s . c o m
 */
private static void addReference(Element cur, Map<Integer, Object> references, Object o) {
    if (cur.hasAttribute(REF_ID)) {
        int id = Integer.parseInt(cur.getAttribute(REF_ID));
        references.put(id, o);
    }
}

From source file:Main.java

public static int getOptionalInt(Element element, String attribute, int defaultValue) {
    if (element.hasAttribute(attribute)) {
        String s = element.getAttribute(attribute);

        if (s.startsWith("+")) {
            s = s.substring(1);//from w w  w  . ja  v  a 2s. c  om
        }

        return Integer.parseInt(s);
    }
    return defaultValue;
}

From source file:Main.java

/**
 * Return the first child Element with the given name; if name is null returns the first element.
 *///from w  w  w.j ava2  s .  c o  m
public static Element firstChildElement(Element element, String childElementName, String attrName,
        String attrValue) {
    if (element == null)
        return null;
    // get the first element with the given name
    Node node = element.getFirstChild();

    if (node != null) {
        do {
            if (node.getNodeType() == Node.ELEMENT_NODE
                    && (childElementName == null || childElementName.equals(node.getNodeName()))) {
                Element childElement = (Element) node;

                String value = childElement.getAttribute(attrName);

                if (value != null && value.equals(attrValue)) {
                    return childElement;
                }
            }
        } while ((node = node.getNextSibling()) != null);
    }
    return null;
}

From source file:Main.java

/**
 * Looks through all child elements of the specified root (recursively) and
 * returns the elements that corresponds to all parameters.
 *
 * @param root the Element where the search should begin
 * @param tagName the name of the node we're looking for
 * @param keyAttributeName the name of an attribute that the node has to
 *            have//from  w  ww  . j av  a 2  s.c  o m
 * @param keyAttributeValue the value that attribute must have
 * @return list of Elements in the tree under root that match the specified
 *         parameters.
 * @throws NullPointerException if any of the arguments is null.
 */
public static List<Element> locateElements(Element root, String tagName, String keyAttributeName,
        String keyAttributeValue) {
    List<Element> result = new ArrayList<Element>();
    NodeList nodes = root.getChildNodes();
    Node node;
    int len = nodes.getLength();
    for (int i = 0; i < len; i++) {
        node = nodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        // is this the node we're looking for?
        if (node.getNodeName().equals(tagName)) {
            Element element = (Element) node;
            String attr = element.getAttribute(keyAttributeName);

            if (attr != null && attr.equals(keyAttributeValue))
                result.add(element);
        }

        // look inside.

        List<Element> childs = locateElements((Element) node, tagName, keyAttributeName, keyAttributeValue);

        if (childs != null)
            result.addAll(childs);

    }

    return result;
}

From source file:Main.java

public static int attributeIntValue(Element e, String attr, Integer defaultValue) {
    if (!e.hasAttribute(attr)) {
        return defaultValue;
    }//  w w  w. j a  va2  s. co  m
    if (e.getAttribute(attr).equals("yes") || e.getAttribute(attr).equals("true")) {
        return 1;
    }
    return Integer.parseInt(e.getAttribute(attr));
}

From source file:Main.java

public static Integer attributeIntValue(Element e, String attr, Integer defaultValue) {
    if (!e.hasAttribute(attr)) {
        return defaultValue;
    }/*  w  w  w .j a  va2  s  . com*/
    if (e.getAttribute(attr).equals("yes") || e.getAttribute(attr).equals("true")) {
        return 1;
    }
    return Integer.parseInt(e.getAttribute(attr));
}

From source file:Utils.java

/**
 * Recursive method to find a given attribute value
 *///from  w  w  w.  ja va2  s. com
public static String recursiveGetAttributeValue(Element element, String attributeName) {
    String answer = null;
    try {
        answer = element.getAttribute(attributeName);
    } catch (Exception e) {

    }
    if (answer == null || answer.length() == 0) {
        Node parentNode = element.getParentNode();
        if (parentNode instanceof Element) {
            return recursiveGetAttributeValue((Element) parentNode, attributeName);
        }
    }
    return answer;
}

From source file:Main.java

public static String getAttributeFromElement(Element element, String name, String attributeName) {
    String result = null;/*  ww w  .jav a2s  .  com*/
    Element nameElement = (Element) element.getElementsByTagName(name).item(0);
    if (nameElement != null) {
        result = nameElement.getAttribute(attributeName).toString();
    }
    if (result != null && result.length() == 0) {
        result = null;
    }
    return result;
}