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 List<Element> getChildNodes(Element parent) {
    NodeList list = parent.getChildNodes();
    List<Element> children = new ArrayList<Element>(list.getLength());
    for (int i = 0; i < list.getLength(); i++) {
        Node child = list.item(i);
        if (child.getNodeType() == 1)
            children.add((Element) child);
    }//from w w  w  .  ja v  a 2 s . c o m

    return children;
}

From source file:Main.java

public static List<Element> getChildNodes(Element parent, String name) {
    NodeList list = parent.getElementsByTagName(name);
    List<Element> children = new ArrayList<Element>(list.getLength());
    for (int i = 0; i < list.getLength(); i++) {
        Node child = list.item(i);
        if (child.getNodeType() == 1)
            children.add((Element) child);
    }/*from  w w w.j a  v  a2  s .  co  m*/

    return children;
}

From source file:Main.java

public static final List<Element> getChildElements(Element parent) {
    final List<Element> childElements = new ArrayList<Element>();

    final NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); ++i) {
        final Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) node);
        }//w  w  w.j  a va2 s .c om
    }

    return childElements;
}

From source file:Main.java

public static String getTextContent(Node node) {
    StringBuffer buffer = new StringBuffer();
    NodeList childList = node.getChildNodes();
    for (int i = 0; i < childList.getLength(); i++) {
        Node child = childList.item(i);
        if (child.getNodeType() != Node.TEXT_NODE)
            continue; // skip non-text nodes
        buffer.append(child.getNodeValue());
    }// ww w  .  j ava  2  s .  c om
    return buffer.toString();
}

From source file:Main.java

public static String getNodeText(Node node) {
    if (node == null)
        return null;
    StringBuffer buff = new StringBuffer();
    for (int c = 0; c < node.getChildNodes().getLength(); c++) {
        Node cn = node.getChildNodes().item(c);
        if (cn.getNodeType() == Node.TEXT_NODE || cn.getNodeType() == Node.CDATA_SECTION_NODE) {
            buff.append(cn.getNodeValue());
        }//from w  ww  .j  a  va 2 s.  c  o  m
    }
    return buff.toString().trim();

}

From source file:Main.java

public static Node getChildNode(Node parentNode, String nodeName) {
    Node node = null;//from   w ww  .j a  v  a2s.  c o m
    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)) {
                node = childNode;
                break;
            }
        }
    }
    return node;
}

From source file:Main.java

private static DOMImplementation getDocImplementation(Node node) {
    DOMImplementation domImpl;//from   w w w  . ja  v a2s .co  m
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        domImpl = ((Document) node).getImplementation();
    } else {
        domImpl = node.getOwnerDocument().getImplementation();
    }
    return domImpl;
}

From source file:Main.java

/**
 * Gets first child element with specified name.
 *
 * @param parentNode parent node.//www  . j  a  va  2s  .c o  m
 * @param childName  child name.
 * @return first childr element with specified name.
 */
public static Element getChildElement(final Node parentNode, final String childName) {
    //        parentNode.normalize();
    final NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(childName))
            return (Element) node;
    }
    return null;
}

From source file:Main.java

private static void registerIDs(Document doc, Node node, Map<String, Node> idMap) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        if (node.getAttributes().getNamedItem("id") != null) {
            final String id = node.getAttributes().getNamedItem("id").getNodeValue();
            idMap.put(id, (Element) node);
        }/*  ww w. ja  va 2  s.co  m*/
    }
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        registerIDs(doc, children.item(i), idMap);
    }

}

From source file:Main.java

public static String[] getChildrenText(Element parentElement, String childrenName) {
    NodeList nl = parentElement.getChildNodes();
    int max = nl.getLength();
    LinkedList<String> list = new LinkedList<String>();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) {
            list.add(getText((Element) n));
        }/* w  ww. ja  v a 2  s  .  com*/
    }
    return list.toArray(new String[list.size()]);
}