Example usage for org.w3c.dom Element getNodeName

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

Introduction

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

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

From source file:Main.java

/**
 * Returns true if the given value is an XML node with the node name and if
 * the optional attribute has the specified value.
 * // ww w .  j  a  v  a2  s  .  c  o  m
 * @param value
 *            Object that should be examined as a node.
 * @param nodeName
 *            String that specifies the node name.
 * @param attributeName
 *            Optional attribute name to check.
 * @param attributeValue
 *            Optional attribute value to check.
 * @return Returns true if the value matches the given conditions.
 */
public static boolean isNode(Object value, String nodeName, String attributeName, String attributeValue) {
    if (value instanceof Element) {
        Element element = (Element) value;

        if (nodeName == null || element.getNodeName().equalsIgnoreCase(nodeName)) {
            String tmp = (attributeName != null) ? element.getAttribute(attributeName) : null;

            return attributeName == null || (tmp != null && tmp.equals(attributeValue));
        }
    }

    return false;
}

From source file:Main.java

public static List<Element> elements(Element element, String tagName) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }// w  w w  . j a va2s . c  o m

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            String childTagName = childElement.getNodeName();
            if (tagName.equals(childTagName))
                elements.add(childElement);
        }
    }
    return elements;
}

From source file:Main.java

/**
 * Returns the boolean value of the text content for the child element with the specified name
 * for the specified element.//from   www . j a va2  s .  co m
 *
 * @param element the parent element
 * @param name    the name of the child element to return
 *
 * @return the boolean value of the text content for the child element or <code>null</code> if a
 *         child element with the specified name could not be found
 */
public static Boolean getChildElementBoolean(Element element, String name) {
    NodeList nodeList = element.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);

        if (node instanceof Element) {
            Element childElement = (Element) node;

            if (childElement.getNodeName().equals(name)) {
                try {
                    return Boolean.parseBoolean(childElement.getTextContent());
                } catch (Throwable e) {
                    throw new RuntimeException("Failed to parse the invalid boolean value ("
                            + childElement.getTextContent() + ")");
                }
            }
        }
    }

    return null;
}

From source file:Main.java

public static boolean hasNamedChild(Element e, String name) {
    Element c = getFirstChild(e);
    while (c != null && !name.equals(c.getLocalName()) && !name.equals(c.getNodeName()))
        c = getNextSibling(c);//from  ww  w  .ja va 2 s .  co  m
    return c != null;
}

From source file:Main.java

public static Element getChildElement(Element parent, String childName) {
    NodeList children = parent.getChildNodes();
    int size = children.getLength();

    for (int i = 0; i < size; i++) {
        Node node = children.item(i);

        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;

            if (childName.equals(element.getNodeName())) {
                return element;
            }/*from   w  w w.  j  av a  2s.  com*/
        }
    }

    return null;
}

From source file:Main.java

/**
 * Get child elements by name, ignoring namespace declarations
 *
 * @param ele parent element//  w  w w.  j  a va  2 s.  c o m
 * @param name name of elements to find
 */
public static List<Element> getElementsByName(Element ele, String name) {
    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);
        String n = e.getNodeName();
        if (matches(n, name))
            list.add(e);
    }
    return (list);
}

From source file:Main.java

public static String getXPath(Element elt) {
    String xpath = "";
    if (elt != null) {
        Document doc = elt.getOwnerDocument();
        Element root = doc.getDocumentElement();
        Element parent = elt;
        while (parent != root) {
            xpath = "/" + parent.getNodeName() + xpath;
            parent = (Element) parent.getParentNode();
        }//w ww  .  j  av a  2  s  . co  m
    }
    return xpath;
}

From source file:Main.java

public static Element getNamedChild(Element e, String name) {
    Element c = getFirstChild(e);
    while (c != null && !name.equals(c.getLocalName()) && !name.equals(c.getNodeName()))
        c = getNextSibling(c);/*from  www  . j  av a2 s.c  o  m*/
    return c;
}

From source file:Main.java

public static List<Element> getSubChildElementList(Element ele, String[] tagnames) {
    if (ele == null) {
        return null;
    }//from  www .  j  av a 2s .c om

    List<Element> v = new ArrayList<Element>();

    // boolean isall = false;

    NodeList tmpnl = ele.getChildNodes();

    Node tmpn = null;

    int k;
    for (k = 0; k < tmpnl.getLength(); k++) {
        tmpn = tmpnl.item(k);

        if (tmpn.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }

        Element eee = (Element) tmpn;
        String noden = eee.getNodeName();
        int p = noden.indexOf(':');
        if (p >= 0)
            noden = noden.substring(p + 1);

        for (String tagname : tagnames) {
            if (tagname.equals(noden) || tagname.equals("*")) {
                v.add(eee);
                break;
            }
        }
    }

    return v;
}

From source file:Main.java

public static Element[] getSubChildElement(Element ele, String[] tagnames) {
    if (ele == null) {
        return null;
    }//from  ww  w  . ja v a  2s  .c o m

    List<Element> v = new ArrayList<Element>();

    NodeList tmpnl = ele.getChildNodes();

    Node tmpn = null;

    int k;
    for (k = 0; k < tmpnl.getLength(); k++) {
        tmpn = tmpnl.item(k);

        if (tmpn.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }

        Element eee = (Element) tmpn;
        String noden = eee.getNodeName();
        int p = noden.indexOf(':');
        if (p >= 0)
            noden = noden.substring(p + 1);

        for (int i = 0; i < tagnames.length; i++) {
            if (tagnames[i].equals(noden) || tagnames[i].equals("*")) {
                v.add(eee);
                break;
            }
        }
    }

    Element[] rets = new Element[v.size()];
    v.toArray(rets);
    return rets;
}