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 Element getPropertyNode(Element properties, String name) {
    NodeList nodeList = properties.getElementsByTagName(PROPERTY_NODE);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element element = (Element) nodeList.item(i);
        if (name.equals(element.getAttributes().getNamedItem(NAME_ATTR).getNodeValue()))
            return element;
    }/*from  w w w .  j  ava2 s  .  c om*/
    return null;
}

From source file:Main.java

static public Element getElementByName(Element parent, String nodeName) {
    NodeList nodes = parent.getElementsByTagName(nodeName);
    if (nodes.getLength() == 0)
        throw new IllegalArgumentException("Cannot find node " + nodeName);

    return ((Element) nodes.item(0));
}

From source file:Main.java

static public Element getElementByName(Element parent, String nodeName) {
    NodeList nodes = parent.getElementsByTagName(nodeName);
    if (nodes.getLength() == 0)
        return null;

    return ((Element) nodes.item(0));
}

From source file:Main.java

static public Element getElementByTagName(Element element, String name, int index) {
    NodeList list = element.getElementsByTagName(name);
    return (Element) list.item(index);
}

From source file:Main.java

public static String getNodeString(Element elem, String name, int inx) {
    NodeList nl = elem.getElementsByTagName(name);
    if (inx >= nl.getLength())
        return null;
    Element element = (Element) nl.item(inx);
    return getSimpleElementText(element);
}

From source file:Main.java

/**
 * Return array of children nodes by node name
 *//*w w  w  .j  a v a  2s.  co  m*/
public static Node[] childrenByNodeName(Element owner, String searchNode) {
    NodeList list = owner.getElementsByTagName(searchNode);
    Node[] children = new Node[list.getLength()];
    for (int i = 0; i < children.length; i++) {
        if (isElement(list.item(i)) && searchNode.equalsIgnoreCase(list.item(i).getLocalName())) {
            children[i] = list.item(i);
        }
    }
    return children;
}

From source file:Main.java

public static String[] getStringValuesForXMLTag(Element xmlElement, String key) {
    NodeList nl = xmlElement.getElementsByTagName(key);

    if (nl.getLength() > 0) {

        String[] output = new String[nl.getLength()];

        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i).getFirstChild();
            if (node != null) {
                output[i] = node.getNodeValue().trim();
            }/*w w  w.  ja v a2 s .  c  o  m*/
        }
        return output;
    }
    return null;
}

From source file:Main.java

/**
 * Get string value for a tag/*from   www . java 2 s  .c  o  m*/
 * 
 * @param xmlElement
 *            Root Element of subtree in which required tag is to be
 *            searched
 * @param key
 *            Required tage name
 * @return String value
 */
public static String getStringValueForXMLTag(Element xmlElement, String key) {
    NodeList nl = xmlElement.getElementsByTagName(key);
    String output = "nothing";
    if (nl.getLength() > 0) {
        Node node = nl.item(0).getFirstChild();
        if (node != null) {
            output = node.getNodeValue().trim();
            return output;
        }
    }
    return output;
}

From source file:Main.java

public static Element[] getElements(Element parentElement, String nodeName) {
    NodeList list = parentElement.getElementsByTagName(nodeName);
    if (list == null) {
        return new Element[0];
    }/*from w w  w  . j  ava2  s . c  o m*/
    Element[] eles = new Element[list.getLength()];
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        eles[i] = (Element) node;
    }
    return eles;
}

From source file:Main.java

/**
 *//*  w  w w  .j av a  2 s . c  o  m*/
public static ArrayList<Element> getElementsByTag(String tag, Element searchIn) {
    NodeList list = searchIn.getElementsByTagName(tag);
    ArrayList<Element> toReturn = new ArrayList<Element>();
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        if (!(n instanceof Element)) {
            continue;
        }
        toReturn.add((Element) n);
    }
    return toReturn;
}