Example usage for org.w3c.dom Element getElementsByTagName

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

Introduction

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

Prototype

public NodeList getElementsByTagName(String name);

Source Link

Document

Returns a NodeList of all descendant Elements with a given tag name, in document order.

Usage

From source file:Main.java

public static void loadAttributesFromNode(Node node, Properties attrs) {
    attrs.clear();/*from  w  w w .java 2 s .c om*/
    Element elem = (Element) node;
    NodeList list = elem.getElementsByTagName(TAG_ATTR);
    for (int i = 0; i < list.getLength(); i++) {
        String text = getTextTag(list.item(i));
        String[] s = text.split("=");
        if (s.length >= 2) {
            attrs.setProperty(s[0], s[1]);
        }
    }
}

From source file:Main.java

private static List<Element> getDirectChildren(Element element, String tagName) {
    NodeList nList = element.getElementsByTagName(tagName);
    List<Element> list = new ArrayList<Element>();
    int listLength = nList.getLength();
    for (int i = 0; i < listLength; i++) {
        Element listElement = (Element) nList.item(i);
        if (listElement.getParentNode() == element) {
            list.add(listElement);/*from  w w w. j a  v  a 2  s.  c o m*/
        }
    }
    return list;
}

From source file:Main.java

/**
 * Return the first child element with a given name.  
 * @param parent parent element/*from   ww w. ja  va 2s .c  o m*/
 * @param name child element name
 * @return 
 */
public static Element getFirstChildByName(Element parent, String name) {
    NodeList children = parent.getElementsByTagName(name);
    if (children.getLength() == 0) {
        return null;
    }
    return (Element) children.item(0);
}

From source file:Main.java

/**
 * Gets the descendant elements list from the parent element.
 *
 * @param parent/*  w  w  w. j av a2s .  c  o m*/
 *          the parent element in the element tree
 * @param tagName
 *          the specified tag name
 * @return the NOT NULL descendant elements list
 */
public static List<Element> getElements(Element parent, String tagName) {
    NodeList nodes = parent.getElementsByTagName(tagName);
    List<Element> elements = new ArrayList<>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            elements.add((Element) node);
        }
    }

    return elements;
}

From source file:Main.java

public static String readStringElement(Element parentElement, String name, int index) {
    Element element = (Element) parentElement.getElementsByTagName(name).item(index);
    String text = element.getTextContent();
    if (text == "null") {
        text = null;/* w w w . j a  va  2 s .c om*/
    }
    return text;
}

From source file:Main.java

public static String getAttribute(Element elem, String attr, String def) {
    NodeList nlist = elem.getElementsByTagName("attribute");
    String value = null;/*w ww . ja v  a 2 s  .co  m*/

    for (int i = 0; i < nlist.getLength(); i++) {
        Element node = (Element) nlist.item(i);
        if (attr.equals(node.getAttribute("name"))) {
            value = node.getTextContent();
            break;
        }
    }

    if (value == null) {
        value = def;
    }
    return value;
}

From source file:Main.java

public static String getSubNodeValue(Element element, String name) {
    NodeList nodeList = element.getElementsByTagName(name);
    return getNodeValue(nodeList.item(0)).trim();
}

From source file:Main.java

public static String getTextValue(Element ele, String tagName) {
    String textVal = null;//from   w  ww .  j a va2s .  co  m
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        textVal = el.getFirstChild().getNodeValue();
    }
    return textVal;
}

From source file:Main.java

public static String getTagValue(String tag, Element elem) {
    String retVal = "";
    NodeList fstNmElmntLst = elem.getElementsByTagName(tag);
    Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    if (fstNmElmnt == null)
        return "";
    NodeList fstNm = fstNmElmnt.getChildNodes();
    if (fstNm == null || fstNm.item(0) == null)
        return "";
    retVal = fstNm.item(0).getNodeValue();
    return retVal;
}

From source file:Main.java

/**
 * 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//from  w  w  w.  j a v  a 2s.  co  m
 * @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));
}