Example usage for org.w3c.dom Node ELEMENT_NODE

List of usage examples for org.w3c.dom Node ELEMENT_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node ELEMENT_NODE.

Prototype

short ELEMENT_NODE

To view the source code for org.w3c.dom Node ELEMENT_NODE.

Click Source Link

Document

The node is an Element.

Usage

From source file:Main.java

public static List<Element> getChildElements(Element parentElement) {
    NodeList nodeList = parentElement.getChildNodes();
    List<Element> childElements = new ArrayList<Element>(nodeList.getLength());
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(0);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) childNode);
        }//  w ww .j  a  v  a  2 s. c o m
    }
    return childElements;
}

From source file:Main.java

public static List getChildrenElement(Node n) {
    List lst = new ArrayList();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);/*from  w w w.  j a  v a2 s .c  o  m*/
        if (cn.getNodeType() == Node.ELEMENT_NODE) {
            lst.add(cn);
        }
    }

    return lst;
}

From source file:Main.java

public static Object getContent(Element element) {
    NodeList nl = element.getChildNodes();
    StringBuffer content = new StringBuffer();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);//w w  w . j  av a  2s. c o m
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            return node;
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            content.append(node.getNodeValue());
            break;
        }
    }
    return content.toString().trim();
}

From source file:Main.java

/**
 * equivalent to the XPath expression './/tagName[@attrName='attrValue']'
 *///from w ww  . ja  v a2 s.co m
public static Element getElementByAttributeValue(final Node start, final String tagName, final String attrName,
        final String attrValue) {
    NodeList nl = ((Element) start).getElementsByTagName(tagName);
    int l = nl.getLength();

    if (l == 0) {
        return null;
    }

    Element e = null;
    String compareValue = null;

    for (int i = 0; i < l; i++) {
        e = (Element) nl.item(i);

        if (e.getNodeType() == Node.ELEMENT_NODE) {
            compareValue = e.getAttribute(attrName);

            if (compareValue.equals(attrValue)) {
                return e;
            }
        }
    }

    return null;
}

From source file:Main.java

public static List<Element> getNamedChildElements(final Element parent, final String name) {
    List<Element> elements = new ArrayList<Element>();
    if (parent != null) {
        Node child = parent.getFirstChild();
        while (child != null) {
            if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(name))) {
                elements.add((Element) child);
            }//from w w w. j  a v  a  2s .co m
            child = child.getNextSibling();
        }
    }
    return elements;
}

From source file:Main.java

/**
 * This method returns a list of the direct element node children of this element node with the specified tag.
 * @param node - parent node/*from  ww w  .  jav a2s.c  o m*/
 * @param tag - tag of direct children to be returned
 * @return a list containing the direct element children with the given tag
 * @author Tristan Bepler
 */
public static List<Element> getDirectChildElementsByTag(Element node, String tag) {
    List<Element> children = new ArrayList<Element>();
    Node child = node.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE
                && (child.getNodeName().equals(tag) || tag.equals(DOM_WILDCARD))) {
            children.add((Element) child);
        }
        child = child.getNextSibling();
    }
    return children;
}

From source file:Main.java

public static void setPrefixRecursive(final Node node, final String prefix) {

    if (node.getNodeType() == Node.ELEMENT_NODE) {
        node.setPrefix(prefix);/*  w  w w  . j av  a 2s  . c o  m*/
    }

    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        setPrefixRecursive(list.item(i), prefix);
    }
}

From source file:Utils.java

/**
 * <p>Returns a list of child elements with the given
 * name. Returns an empty list if there are no such child
 * elements.</p>//from ww  w.  j a  v  a  2s  .  c  o m
 *
 * @param parent parent element
 * @param name name of the child element
 * @return child elements
 */
public static List getChildElementsByName(Element parent, String name) {
    List elements = new ArrayList();

    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)) {
                elements.add(element);
            }
        }
    }

    return elements;
}

From source file:Main.java

public static Element getRootElement(final Node docWithARootElement) {

    if (docWithARootElement != null && docWithARootElement.getChildNodes() != null
            && docWithARootElement.getChildNodes().getLength() > 0) {
        int numOfChildNodes = docWithARootElement.getChildNodes().getLength();

        if (numOfChildNodes == 1) {
            return (Element) docWithARootElement.getFirstChild();
        } else {/*from   w w w.  j  av  a  2  s .  c o  m*/
            for (int i = 0; i < numOfChildNodes; i++) {
                Node nodeToInspect = docWithARootElement.getChildNodes().item(i);

                if (nodeToInspect != null && nodeToInspect.getNodeType() == Node.ELEMENT_NODE) {
                    return (Element) nodeToInspect;
                }
            }
        }
    }

    return null;

}

From source file:Main.java

public static String domNode2String(Node node, boolean escapeStrings) {
    String ret = "";
    switch (node.getNodeType()) {

    case Node.DOCUMENT_NODE:
        // recurse on each child
        NodeList nodes = node.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                ret += domNode2String(nodes.item(i), escapeStrings);
            }/*from  w  w  w . jav a2s.co m*/
        }
        break;

    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        ret += "<" + name;
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            ret += " " + current.getNodeName() + "=\""
                    + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue())
                    + "\"";
        }
        ret += ">";

        // recurse on each child
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                ret += domNode2String(children.item(i), escapeStrings);
            }
        }

        ret += "</" + name + ">";
        break;

    case Node.TEXT_NODE:
        ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue();
        break;
    case Node.COMMENT_NODE:
        ret += "<!--" + node.getNodeValue() + "-->";
        break;
    }
    return ret;
}