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

/** Finds and returns the next sibling node with the given name. */
public static Element getNextSiblingElement(Node node, String elemNames[]) {

    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                if (sibling.getNodeName().equals(elemNames[i])) {
                    return (Element) sibling;
                }//  www .j  ava 2  s .c om
            }
        }
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static Element childNodeByTag(Node node, String childNodeName) {
    if (node == null)
        return null;

    Element childElement = null;// ww  w .  j  a  v a 2s.c o m
    Node childNode = node.getFirstChild();
    if (childNode != null) {
        do {
            if (childNode.getNodeType() == Node.ELEMENT_NODE
                    && (childNodeName == null || childNodeName.equals(childNode.getNodeName()))) {
                return (Element) childNode;
            }
        } while ((childNode = childNode.getNextSibling()) != null);
    }

    return null;
}

From source file:DOMHelper.java

/**
 * Gets the next sibling of a node that is an element.
 * @param node the node//ww  w  .  j  a  v a  2s. co  m
 * @return the next sibling element or {@code null} if none
 * @throws NullPointerException if {@code node} is {@code null}
 */
public static Element getNextSiblingElement(Node node) {
    do {
        node = node.getNextSibling();
    } while (node != null && node.getNodeType() != Node.ELEMENT_NODE);

    return (Element) node;
}

From source file:Main.java

/**
 * Finds and returns the next sibling node with the given name and
 * attribute name, value pair. Since only elements have attributes,
 * the node returned will be of type Node.ELEMENT_NODE.
 *//*from w ww  .  j ava2 s  . c  om*/
public static Element getNextSiblingElement(Node node, String elemName, String attrName, String attrValue) {

    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) sibling;
            if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) {
                return element;
            }
        }
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/** Finds and returns the first child node with the given qualified name. */
public static Element getFirstChildElementNS(Node parent, String[][] elemNames) {

    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                String uri = child.getNamespaceURI();
                if (uri != null && uri.equals(elemNames[i][0])
                        && child.getLocalName().equals(elemNames[i][1])) {
                    return (Element) child;
                }//from   w ww.  jav  a2  s .co  m
            }
        }
        child = child.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * Gets the ancestor element node to the given node.
 * //from   ww w  . j  a  v  a2s .co m
 * @param currentNode the node to retrive the ancestor for
 * 
 * @return the ancestral element node of the current node, or null
 */
public static Element getElementAncestor(Node currentNode) {
    Node parent = currentNode.getParentNode();
    if (parent != null) {
        short type = parent.getNodeType();
        if (type == Node.ELEMENT_NODE) {
            return (Element) parent;
        }
        return getElementAncestor(parent);
    }
    return null;
}

From source file:Main.java

/**
 * The method extractChildNodes extracts child nodes of a specified type.
 *
 * @param aParentNode/*from   ww w . j  av  a 2  s .c o  m*/
 *        a parent node
 * @param aRequiredType
 *        a required node type
 *
 * @return a list of child nodes
 */
static List<Node> extractChildNodes(Node aParentNode, short aRequiredType) {

    List<Node> elementNodes = new ArrayList<Node>();

    NodeList childNodeList = aParentNode.getChildNodes();
    for (int a = 0; a < childNodeList.getLength(); a++) {
        Node node = childNodeList.item(a);
        short nodeType = node.getNodeType();
        if (nodeType == aRequiredType) {
            elementNodes.add(node);
        }
    }

    return elementNodes;
}

From source file:Main.java

/**
 * Check whether the given nodes containes an attribute with the given name and value.<br>
 * The "matching" is assumed to be true even if the attribute just starts or ends with the given
 * attribute value.<br>//from   w w w.  j av  a  2 s  . c o  m
 * This has been allowed to match multiple keyword attributes such as in 
 * <code>class="hfeed hentry"</code> cases.<br>
 * 
 * @param node node to be analyzed
 * @param attrName the attribute name to be matched
 * @param attrValue the attribute value to be matched
 * @return <code>true</code> if node containes the given attributes, <code>false</code> otherwise (even if node is null)
 */
public static boolean nodeAttributeMatches(Node node, String attrName, String attrValue) {

    if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {

        String value = ((Element) node).getAttribute(attrName);
        return (value != null && value.length() > 0 && attributeValueMatches(value, attrValue));

    } else {

        return false;

    }
}

From source file:Main.java

public static String getNamespace(String prefix, Node e, Node stopNode) {
    while (e != null && (e.getNodeType() == Node.ELEMENT_NODE)) {
        Attr attr = null;/*from  w  w  w . ja va  2  s.  co m*/
        if (prefix == null) {
            attr = ((Element) e).getAttributeNode("xmlns");
        } else {
            attr = ((Element) e).getAttributeNodeNS(NS_URI_XMLNS, prefix);
        }
        if (attr != null)
            return attr.getValue();
        if (e == stopNode)
            return null;
        e = e.getParentNode();
    }
    return null;
}

From source file:Main.java

/**
 * Access all immediate child elements inside the given Element
 *
 * @param element the starting element, cannot be null.
 * @param elemName the name of the child element to look for, cannot be
 * null.//from ww  w  .j a  v a2 s  .  co  m
 * @return array of all immediate child element inside element, or
 * an array of size zero if no child elements are found.
 */
public static Element[] getChildElements(Element element, String elemName) {
    NodeList list = element.getChildNodes();
    int len = list.getLength();
    ArrayList<Node> array = new ArrayList<Node>(len);

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (elemName.equals(n.getNodeName())) {
                array.add(n);
            }
        }
    }
    Element[] elems = new Element[array.size()];

    return (Element[]) array.toArray(elems);
}