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

/**
 * to get node's owner document/*from  w  w  w  . j a v a 2  s .  c  o  m*/
 *
 * @return The document that contain node
 */
public static Document getOwnerDocument(Node node) {
    Document doc = null;
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        doc = (Document) node;
    } else {
        doc = node.getOwnerDocument();
    }
    return doc;
}

From source file:Main.java

public static List<Element> getChildElementsByTagName(Element parentElement, String childTag) {
    NodeList nodelist = parentElement.getChildNodes();
    List<Element> nodes = new ArrayList<Element>();
    for (int i = 0; i < nodelist.getLength(); i++) {
        Node temp = nodelist.item(i);
        if (temp.getNodeType() == Node.ELEMENT_NODE && temp.getNodeName().equals(childTag)) {
            nodes.add((Element) temp);
        }//from ww  w  .j a  v a 2s .co m
    }
    return nodes;
}

From source file:Main.java

public static String getText(Element element) {
    StringBuffer buf = new StringBuffer();
    NodeList list = element.getChildNodes();
    boolean found = false;
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            buf.append(node.getNodeValue());
            found = true;/*from w w  w  .j av a 2 s  .  c  om*/
        }
    }
    return found ? buf.toString() : null;
}

From source file:Main.java

/**
 * Gets the content of a subnode./*from   ww w  . ja va 2 s .  c om*/
 * For example,
 * <item>
 *     <nodeName>content</nodeName>
 * </item>
 */
public static Optional<String> getNodeContent(Node item, String nodeName) {
    if (item.getNodeType() != Node.ELEMENT_NODE) {
        return Optional.empty();
    }

    NodeList metadata = ((Element) item).getElementsByTagName(nodeName);
    if (metadata.getLength() == 1) {
        return Optional.ofNullable(metadata.item(0).getTextContent());
    } else {
        return Optional.empty();
    }
}

From source file:Main.java

public static String getTextContent(Node e) {
    StringBuilder sb = new StringBuilder();

    NodeList children = e.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node n = children.item(i);
            if (n.getNodeType() == Node.TEXT_NODE) {
                sb.append(n.getNodeValue());
            }/*from  ww  w . j a  v  a  2  s.c  om*/
        }
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Indicates whether element has any child element.
 *
 * @param element the namespace to analyze.
 * @return true if element has any child element otherwise false.
 *//*from  w  ww  .j  a va 2s  . c o m*/
public static boolean hasChildElements(Element element) {
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

private static String getText(Node node) {
    if (node == null)
        return null;
    NodeList lst = node.getChildNodes();
    int size = lst.getLength();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            sb.append(t.getData());//  ww  w. j  a va 2  s.c o m
        }
    }
    return sb.toString();
}

From source file:Main.java

public static List<Node> getChildNodeList(Node parentNode, String nodeName) {
    List<Node> childNodeList = new ArrayList<>();
    NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (((Element) childNode).getNodeName().equals(nodeName)) {
                childNodeList.add(childNode);
            }//from w w w  .j a va  2 s . co m
        }
    }
    return childNodeList;
}

From source file:Main.java

public static Element getFirstChildElementIgnoreCase(Element elm, String name) {
    if (elm == null)
        return null;

    NodeList nl = elm.getChildNodes();
    for (int c = 0; c < nl.getLength(); c++) {
        Node node = nl.item(c);
        if (node.getNodeType() == Node.ELEMENT_NODE
                && (name == null || node.getNodeName().equalsIgnoreCase(name)))
            return (Element) node;
    }/* ww w .  j a  v  a 2s .c om*/

    return null;
}

From source file:Main.java

private static void removeEmptyChildElements(Element parentElement) {
    List<Element> toRemove = new LinkedList<Element>();

    NodeList children = parentElement.getChildNodes();
    int childrenCount = children.getLength();
    for (int i = 0; i < childrenCount; ++i) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            removeEmptyChildElements(childElement);
            if (elementIsRedundant(childElement)) {
                toRemove.add(childElement);
            }/*from  w  w w .  j a  v a2s .c om*/
        }
    }

    for (Element childElement : toRemove) {
        parentElement.removeChild(childElement);
    }
    parentElement.normalize();
}