Example usage for org.w3c.dom Node getNodeType

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

Introduction

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

Prototype

public short getNodeType();

Source Link

Document

A code representing the type of the underlying object, as defined above.

Usage

From source file:Main.java

public static Set<Node> populateNodes(Node node, Set<Node> nodes) {
    if (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            nodes.add(node);/*from  w w w  . j  av  a 2 s.co m*/
        }
        populateNodes(node.getNextSibling(), nodes);
    }
    return nodes;
}

From source file:Main.java

public static Object getContent(Element element) {
    NodeList nl = element.getChildNodes();
    StringBuilder content = new StringBuilder();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            return node;
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            content.append(node.getNodeValue());
            break;
        }/*  w ww. java2s .  co  m*/
    }
    return content.toString().trim();
}

From source file:Main.java

/**
 * Get the first direct child element of the passed element.
 *
 * @param aStartNode/*from w w w  . j  a v a2s.  c o  m*/
 *        The element to start searching.
 * @return <code>null</code> if the passed element does not have any direct
 *         child element.
 */
@Nullable
public static Element getFirstChildElement(@Nonnull final Node aStartNode) {
    final NodeList aNodeList = aStartNode.getChildNodes();
    final int nLen = aNodeList.getLength();
    for (int i = 0; i < nLen; ++i) {
        final Node aNode = aNodeList.item(i);
        if (aNode.getNodeType() == Node.ELEMENT_NODE)
            return (Element) aNode;
    }
    return null;
}

From source file:Main.java

/**
 * Remove named nodes of the specified nodeType from the specified node.
 * /*from   w w  w  .  j a  va  2  s. co  m*/
 * @param node the node to be cleaned.
 * @param nodeType the type of nodes to be removed.
 * @param name the name of nodes to be removed.
 */
public static void removeAll(final Node node, final short nodeType, final String name) {
    if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
        node.getParentNode().removeChild(node);
    } else {
        // Visit the children
        final NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeAll(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

public static String getElementText(Element elem) {
    StringBuffer buf = new StringBuffer();
    NodeList nodeList = elem.getChildNodes();
    for (int i = 0, len = nodeList.getLength(); i < len; i++) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            buf.append(n.getNodeValue());
        } else {/*from w ww  .ja  v a  2s.co  m*/
            //TODO see jsf-samples
            //throw new FacesException("Unexpected node type " + n.getNodeType());
        }
    }
    return buf.toString();
}

From source file:Main.java

/**
 * Determines the index (starting at 0) of the given element relative to
 * other element nodes for the same parent.
 *///from w  ww. jav a2s .  c om
public static int getElementPosition(Element element) {
    int num = -1;
    Node node = element;
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            ++num;
        }
        node = node.getPreviousSibling();
    }
    return num;
}

From source file:Main.java

public static String getNodeValue(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int x = 0; x < childNodes.getLength(); x++) {
        Node data = childNodes.item(x);
        if (data.getNodeType() == Node.TEXT_NODE)
            return data.getNodeValue();
    }//from   ww w. j a v  a 2  s  .  com
    return "";
}

From source file:Main.java

public static Vector getElementsByTagAndAttribValue(org.w3c.dom.Element element, String tag, String attrib,
        String val) {
    NodeList desElements = element.getElementsByTagName(tag);
    Vector selElements = new Vector(desElements.getLength() / 10, 10);

    for (int i = 0; i < desElements.getLength(); i++) {
        org.w3c.dom.Node desElement = desElements.item(i);

        if (desElement.getNodeType() == org.w3c.dom.Element.ELEMENT_NODE) {
            NamedNodeMap attributeNodes = desElement.getAttributes();

            org.w3c.dom.Node selAttribNode = attributeNodes.getNamedItem(attrib);

            if (selAttribNode != null && selAttribNode.getNodeValue().equalsIgnoreCase(val)) {
                selElements.add(desElement);
            }//from   w  w  w  .j a  v  a2  s.com
        }
    }

    return selElements;
}

From source file:Main.java

/**
 * Get the next sibling Element of the supplied node.
 *
 * @param node The DOM Node./*from www . ja v a  2s.com*/
 * @return The next sibling element
 */
public static Element getNextSiblingElement(Node node) {
    Node sibling = node.getNextSibling();
    while (sibling != null && sibling.getNodeType() != Node.ELEMENT_NODE) {
        sibling = sibling.getNextSibling();
    }
    return (Element) sibling;
}

From source file:Main.java

public static List<Element> toElementList(NodeList nds) {
    List<Element> list = new ArrayList<Element>();
    for (int i = 0; i < nds.getLength(); i++) {
        Node child = nds.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            list.add((Element) child);
        }/*from  ww w.ja  va 2  s  .co m*/
    }
    return list;
}