Example usage for org.w3c.dom Node getChildNodes

List of usage examples for org.w3c.dom Node getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Node getChildNodes.

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:Main.java

public static Node getChildNodebyName(Node parentNode, String nameOfChildToFind) {
    NodeList childNodes = parentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node currentChildNode = childNodes.item(i);
        String childName = currentChildNode.getLocalName();
        if (childName != null)
            if (childName.equals(nameOfChildToFind))
                return currentChildNode;
    }//from ww  w  .  j  ava2s .  com
    return null;
}

From source file:Main.java

public static Map<String, String> SimpleDocumentToMap(Document doc) {
    Map<String, String> simpleMap = new HashMap<String, String>();
    //trim off outter layer
    Node node = doc.getFirstChild();
    NodeList nList = node.getChildNodes();
    for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null) {
                simpleMap.put(eElement.getTagName(), eElement.getTextContent());
            } // end if eelement
        } // end if nnode.getnodetype()
    } //end for int temp
    return simpleMap;
}

From source file:Main.java

/**
 * Get the value of this node. Will return "" instead of null.
 * @return java.lang.String//from ww w  . j  ava 2 s. com
 * @param node org.w3c.dom.Node
 */
public static String getNodeValue(Node node) {
    NodeList nodeList = node.getChildNodes();

    int length = nodeList.getLength();
    for (int i = 0; i < length; i++) {
        Node n = nodeList.item(i);
        if (n instanceof Text) {
            Text t = (Text) n;
            return t.getNodeValue();
        }
    }
    return "";
}

From source file:Main.java

public static Node[] getChildNodes(Node node, short type) {
    NodeList children = node.getChildNodes();
    if (children == null) {
        return new Node[0];
    }/*from   w  w  w.j  a v  a2  s . c  o m*/
    int n = children.getLength();
    List<Node> elnodelist = new ArrayList<Node>(n);
    for (int i = 0; i < n; ++i) {
        Node childnode = children.item(i);
        if (childnode.getNodeType() == type) {
            elnodelist.add(childnode);
        }
    }
    Node[] empty = {};
    return elnodelist.toArray(empty);
}

From source file:Main.java

/**
 * Parse the childnodes of a node and look for &lt;property&gt; elements with attributes name and value.
 * Example:/*from w w  w . j a  v a  2s.  com*/
 * &lt;node&gt;
 *    &lt;property name='n' value='v'/&gt;
 * &lt;node/&gt;
 * @param n the XML node
 * @return the parsed properties
 */
public static Map<String, String> parseProperties(Node n) {
    NodeList propertyNodes = n.getChildNodes();
    Map<String, String> properties = new HashMap<String, String>();

    for (int i = 0; i < propertyNodes.getLength(); i++) {
        Node node = propertyNodes.item(i);

        if (node.getNodeName().equals("property")) {
            try {
                String key = node.getAttributes().getNamedItem("name").getNodeValue();
                String value = node.getAttributes().getNamedItem("value").getNodeValue();

                properties.put(key, value);
            } catch (NullPointerException e) {
                continue;
            }
        }
    }

    return properties;
}

From source file:Main.java

public static boolean isElementNodeExist(Node root, String nodeString) {
    NodeList nlist = root.getChildNodes();

    for (int i = 0; i < nlist.getLength(); i++) {
        if (nlist.item(i) instanceof Element) {
            if (nlist.item(i).getNodeName().equalsIgnoreCase(nodeString)) {
                return true;
            }/*from ww w.  ja v  a 2s  .  c o m*/

            if (nlist.item(i).hasChildNodes() && isElementNodeExist(nlist.item(i), nodeString)) {
                return true;
            }
        }
    }

    return false;
}

From source file:Main.java

public static List<Node> getChildrenbyNodeName(Node parentNode, String nameOfChildrenToFind) {
    NodeList childNodes = parentNode.getChildNodes();
    List<Node> listToReturn = new ArrayList<Node>();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node currentChildNode = childNodes.item(i);
        if (currentChildNode.getLocalName().equals(nameOfChildrenToFind))
            listToReturn.add(currentChildNode);
    }//from w w  w  . j  a va  2s. c om
    return listToReturn;
}

From source file:Main.java

private static void setPrefixRecursively(Node node, String prefix) {
    node.setPrefix(prefix);/*w ww  . j a  v  a2s.  c  o  m*/
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        setPrefixRecursively(node.getChildNodes().item(i), prefix);
    }
}

From source file:Main.java

/**
 * Get the first node with a given name and returns it
 *
 * @param root the node that contains the children to search within
 * @param name the name of the node//w w w  .j a  va  2  s.  c om
 * @return a node with given or null if not found
 */
public static Node find(Node root, String name) {
    final NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        final Node subnode = list.item(i);
        if (subnode.getNodeType() == Node.ELEMENT_NODE) {
            if (subnode.getNodeName().equals(name)) {
                return subnode;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns text content of a given Node.
 *///w  w w  .  j ava 2  s . c o  m
static String getText(Node node) {

    NodeList nodes = node.getChildNodes();
    int len = nodes.getLength();

    if (len == 0) {
        return null;
    }

    StringBuilder text = new StringBuilder();
    for (int i = 0; i < len; i++) {
        Node child = nodes.item(i);

        if (child instanceof CharacterData) {
            text.append(((CharacterData) child).getData());
        }
    }

    return text.length() == 0 ? null : text.toString();
}