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 Element getElementByTagNameAndAttributeValue(Element element, String tagName, String attrName,
        String attrValue) {/*w w  w  .  j av  a2  s. c o m*/
    NodeList nodeList = element.getElementsByTagName(tagName);
    if (nodeList.getLength() == 0) {
        return null;
    }
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element nodeElement = (Element) node;
            if (attrValue.equals(getAttributeValueByName(nodeElement, attrName))) {
                return nodeElement;
            }
        }
    }
    return null;
}

From source file:Main.java

public static Element getElement(Element parentElement, String nodeName, String id) {
    NodeList nodeList = parentElement.getElementsByTagName(nodeName);
    int i;//from  w w  w .ja  v  a 2 s.  c  o m
    for (i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (id.equals(element.getAttribute(idName))) {
                return element;
            }
        }
    }
    return null;
}

From source file:Main.java

public static int indexOf(NodeList nodeList, String tagName, int startIdx) {
    for (int i = startIdx; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(tagName)) {
                return i;
            }/*from  w w w  .ja  v a 2 s  . c om*/
        }
    }
    return -1;
}

From source file:Main.java

/**
 * Get the text content of an element. If the element contains
 * mixed content (both elements and text), only the first text section
 * is returned./*from  w ww.j  a v a2 s  .  co m*/
 *
 * @param element target element to retrieve text on, cannot be null.
 * @return text content of the element.
 */
public static String getElementText(Element element) {
    element.normalize();
    NodeList list = element.getChildNodes();
    int len = list.getLength();

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

        if (n.getNodeType() == Node.TEXT_NODE) {
            String s = n.getNodeValue();

            if (s != null) {
                return s.trim();
            }
        }
    }
    return null;
}

From source file:Main.java

public static String textContent(Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        StringBuffer sb = new StringBuffer();
        Node nextChild = node.getFirstChild();
        while (nextChild != null) {
            sb.append(textContent(nextChild));
            nextChild = nextChild.getNextSibling();
        }/*from w w  w  . ja  v a  2s  .com*/
        return sb.toString();
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        return node.getNodeValue();
    default:
        return "";
    }
}

From source file:Main.java

public static final String getCDATA(Element elem, boolean trim) {
    StringBuilder sb = new StringBuilder();
    NodeList nl = elem.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nc = nl.item(i);
        if (nc.getNodeType() == Node.CDATA_SECTION_NODE) {
            sb.append(((Text) nc).getData());
        } else if (nc.getNodeType() == Node.TEXT_NODE) {
            String txt = ((Text) nc).getData();
            if (trim) {
                txt = txt.trim();/*from w  w  w  . j a  va2 s. c  o m*/
            }
            sb.append(txt);
        }
    }
    return sb.toString();
}

From source file:Main.java

public static List<Element> getElementsByTagNames(Element element, String[] tagNames) {
    List<Element> children = new ArrayList<Element>();
    if (element != null && tagNames != null) {
        List tagList = Arrays.asList(tagNames);
        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE && tagList.contains(((Element) child).getTagName())) {
                children.add((Element) child);
            }/*from  w w  w .  ja  va 2 s  .c  om*/
        }
    }
    return children;
}

From source file:Main.java

/**
 * Method getStrFromNode/*from  w w  w . ja v a  2s  . com*/
 *
 * @param xpathnode
 * @return the string for the node.
 */
public static String getStrFromNode(Node xpathnode) {
    if (xpathnode.getNodeType() == Node.TEXT_NODE) {
        // we iterate over all siblings of the context node because eventually,
        // the text is "polluted" with pi's or comments
        StringBuilder sb = new StringBuilder();

        for (Node currentSibling = xpathnode.getParentNode()
                .getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) {
            if (currentSibling.getNodeType() == Node.TEXT_NODE) {
                sb.append(((Text) currentSibling).getData());
            }
        }

        return sb.toString();
    } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
        return xpathnode.getNodeValue();
    } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return xpathnode.getNodeValue();
    }

    return null;
}

From source file:Main.java

public static List<Node> extractChildElements(Node parent) {
    List<Node> result = new ArrayList<Node>();
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            result.add(child);/*from  w  w w  .j a  va 2s . c o m*/
        }
    }
    return result;
}

From source file:Main.java

/**
 * Gets the next comment.//from w  ww  .ja v  a 2 s  . c  om
 * 
 * @param element
 *            the element
 * @return the next comment
 */
public static String getNextComment(Node element) {
    while (element.getNextSibling() != null) {
        Node prev = element.getNextSibling();
        if (prev.getNodeType() == Node.COMMENT_NODE) {
            return prev.getTextContent();
        } else if (prev.getNodeType() == Node.TEXT_NODE) {
            return getNextComment(prev);
        } else if (prev.getNodeType() == Node.ELEMENT_NODE) {
            return null;
        }
    }
    return null;
}