Example usage for org.w3c.dom Element getTagName

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

Introduction

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

Prototype

public String getTagName();

Source Link

Document

The name of the element.

Usage

From source file:Main.java

public static Element getChild(Element element, String tagName) {
    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            Element child = (Element) node;
            if (child.getTagName().equals(tagName))
                return child;
        }/*w w  w .  j a va2  s .co m*/
    }
    return null;
}

From source file:Main.java

/**
 * Returns a list of the direct child {@link Element}s of the given parent
 * that have the specified tag name.//from   ww w .  j a v a 2  s .  co  m
 * 
 * @param parent The parent element.
 * @param tagName The tag name to filter.
 */
public static List<Element> getChildElementsByTagName(Element parent, String tagName) {
    List<Element> childElementsWithTagName = new LinkedList<Element>();
    List<Element> childElements = getChildElements(parent);
    for (Element child : childElements) {
        if (child.getTagName().equals(tagName))
            childElementsWithTagName.add(child);
    }
    return childElementsWithTagName;
}

From source file:Main.java

private static boolean equalElements(final Element a, final Element b) {
    if (!a.getTagName().equals(b.getTagName())) {
        return false;
    }/*from w  ww . j a  va 2 s  .  c  om*/
    final NamedNodeMap attributes = a.getAttributes();
    int customAttributeCounter = 0;
    for (int i = 0, n = attributes.getLength(); i < n; i++) {
        final Node node = attributes.item(i);
        if (node != null && !node.getNodeName().startsWith("_")) {
            if (!node.getNodeName().equals("z") && (b.getAttribute(node.getNodeName()).length() == 0
                    || !b.getAttribute(node.getNodeName()).equals(node.getNodeValue()))) {
                return false;
            }
        } else {
            customAttributeCounter++;
        }
    }
    if (a.getAttributes().getLength() - customAttributeCounter != b.getAttributes().getLength()) {
        return false;
    }
    return true;
}

From source file:Main.java

public static List<Element> getChildrenWithTag(Element parent, String tag) {
    List<Element> list = getElementChildren(parent);
    for (Element e : list.toArray(new Element[0])) {
        if (!e.getTagName().equals(tag))
            list.remove(e);/*from w w  w . j  a v  a 2  s . co  m*/
    }
    return list;
}

From source file:Main.java

public static ArrayList<Element> getChildrenByTagName(Element node, String tagname) {
    NodeList childrenNode = node.getChildNodes();
    ArrayList<Element> matches = new ArrayList<Element>();
    for (int i = 0; i < childrenNode.getLength(); i++) {
        if (childrenNode.item(i) instanceof Element) {
            Element child = (Element) childrenNode.item(i);
            if (child.getTagName().equals(tagname)) {
                matches.add(child);//  www . j av  a 2s .  c om
            }
        }
    }
    return matches;
}

From source file:Main.java

public static List<Element> getChildElementsByTagName(Element element, String name) {

    List<Element> elements = new LinkedList<Element>();

    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) node;
            if (e.getTagName().equals(name)) {
                elements.add(e);//from  w w  w .  jav  a2s .  c  o  m
            }
        }
    }

    return elements;
}

From source file:Utils.java

/**
 * <p>Returns the first child element with the given name. Returns
 * <code>null</code> if not found.</p>
 *
 * @param parent parent element//from w  ww .j  a  v a 2 s .c om
 * @param name name of the child element
 * @return child element
 */
public static Element getChildElementByName(Element parent, String name) {
    NodeList children = parent.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (element.getTagName().equals(name)) {
                return element;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Extracts a bunch of notes from an <code>Element</code>
 *///w  w  w .  j a va2  s. c  o m
protected static List<String> extractNotesFrom(Element element) {
    List<String> list = new ArrayList<String>();

    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (!(node instanceof Element)) {
            continue;
        }

        Element child = (Element) node;
        if (child.getTagName().equals("note")) {
            list.add(extractTextFrom(child));
        }
    }

    return list;
}

From source file:Main.java

/**
 * Gets the first element with the given tag name in the immediate child elements.
 * /*  w  w  w.jav a2s  . c  o m*/
 * @param element
 * @param tagName
 * @return
 */
public static Element getFirstChildElementByTagName(Node element, String tagName) {
    NodeList list = element.getChildNodes();
    int size = list.getLength();
    if (size > 0) {
        for (int i = 0; i < size; i++) {
            Node node = list.item(i);
            if (node instanceof Element) {
                Element e = (Element) node;
                if (e.getTagName().equals(tagName)) {
                    return e;
                }
            }
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> getChildren(Element element, String tagName) {
    List<Element> children = new ArrayList<>();
    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            Element child = (Element) node;
            if (tagName != null && child.getTagName().equals(tagName))
                children.add(child);/* www  . j  a  v  a2s.c  om*/
        }
    }
    return children;
}