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

/**
 * @param node/*from  w w w  .jav a2 s.  c om*/
 * @return true if the given node is of type text or CDATA.
 */
public static boolean isText(Node node) {
    int ntype = node.getNodeType();
    return ntype == Node.TEXT_NODE || ntype == Node.CDATA_SECTION_NODE;
}

From source file:Main.java

/** Return first child with given name within given element. */
public static Element getFirst(Element parent, String name) {
    NodeList nl = parent.getChildNodes();
    for (int i = 0, l = nl.getLength(); i < l; i++) {
        Node curr = nl.item(i);
        if (curr.getNodeType() == Element.ELEMENT_NODE && curr.getNodeName().equals(name))
            return (Element) curr;
    }/*from   ww  w .j  a  va  2s. co m*/

    return null;
}

From source file:Main.java

public static Element getNextElement(Node el) {
    while ((el != null) && (el.getNodeType() != Node.ELEMENT_NODE)) {
        el = el.getNextSibling();/* w w  w. j  av a2 s .c om*/
    }
    return (Element) el;

}

From source file:Main.java

public static void visitRecursively(Node node, BufferedWriter bw) throws Exception {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        // get child node
        Node childNode = list.item(i);
        if (childNode.getNodeType() == Node.TEXT_NODE) {
            System.out.println("Found Node: " + childNode.getNodeName() + " - with value: "
                    + childNode.getNodeValue() + " Node type:" + childNode.getNodeType());

            String nodeValue = childNode.getNodeValue();
            nodeValue = nodeValue.replace("\n", "").replaceAll("\\s", "");
            if (!nodeValue.isEmpty()) {
                System.out.println(nodeValue);
                bw.write(nodeValue);//  w  w  w .  j  a  v a  2  s  . c  om
                bw.newLine();
            }
        }
        visitRecursively(childNode, bw);
    }
}

From source file:Main.java

public static String getText(Node elem) {
    if (elem.getNodeType() == Element.TEXT_NODE)
        return elem.getTextContent();
    else if (elem.hasChildNodes())
        return elem.getFirstChild().getTextContent();
    return "";
}

From source file:Main.java

public static Node getFirstChildByTagName(Node parent, String tagName) {
    NodeList nodeList = parent.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equalsIgnoreCase(tagName))
            return node;
    }/*from w ww .j  a v  a2 s  .  co m*/
    return null;
}

From source file:Main.java

public static String getFirstLevelTextContent(Node node) {
    NodeList list = node.getChildNodes();
    StringBuilder textContent = new StringBuilder();
    for (int i = 0; i < list.getLength(); ++i) {
        Node child = list.item(i);
        if (child.getNodeType() == Node.TEXT_NODE)
            textContent.append(child.getTextContent());
    }/*from w w  w  .  ja  v a 2s  .  co m*/
    return textContent.toString().trim();
}

From source file:Main.java

public static Element getFirstChild(Element parent, String name) {
    NodeList list = parent.getElementsByTagName(name);
    for (int i = 0; i < list.getLength(); i++) {
        Node child = list.item(i);
        if (child.getNodeType() == 1)
            return (Element) child;
    }//  ww w  .  j  a  v  a 2  s.c  o m

    return null;
}

From source file:Main.java

public static Element getChildElement(Element elem, String name) {
    NodeList nodes = elem.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            return (Element) node;
        }/*from w w  w .  ja v  a 2s.  c o m*/
    }
    return null;
}

From source file:Main.java

public static void setAttribute(Node node, String name, String value) {
    if (node.getNodeType() != Node.ELEMENT_NODE)
        return;/*from   w ww  .  j  a  v a2 s.  c  o  m*/
    Element e = (Element) node;
    e.setAttribute(name, value);
}