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

/**
 *  Returns first node at the bottom of path from node.
 * If element begins with '@', indicates an attribute, eg "@id"
 * The '#text' element indicates that the node has a single text child.
 * @param node    Node to apply path to/*from  ww  w  .ja  va 2  s.  co m*/
 * @param path    Path to apply
 * @return        Node at bottom of path, or null
 */
static public Node extractNode(Node node, String path) {
    if (node == null)
        return null;
    NodeList list = node.getChildNodes();
    if (path.equals("#text"))
        return node.getFirstChild();
    else if (path.charAt(0) == '@')
        return node.getAttributes().getNamedItem(path.substring(1));
    else
        for (int j = 0; j < list.getLength(); j++)
            if (list.item(j).getNodeType() == Node.ELEMENT_NODE && list.item(j).getNodeName().equals(path))
                return list.item(j);

    return null;
}

From source file:Main.java

public static List<Element> readChildren(Node parentNode) {
    ArrayList<Element> ret = new ArrayList<Element>();
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Element)
            ret.add((Element) children.item(i));
    }/*from  w w  w.  ja va 2s  .  co  m*/
    return ret;
}

From source file:Main.java

/**
 * Get the a specific child node from its parent node by matching the local
 * name. If not found, return null./* w ww.  ja v a  2  s .  c  o m*/
 *
 * @param parentNode
 *          the parent node.
 * @param localNodeName
 *          the local name of the node.
 * @return the node with the matching local name; otherwise, null if not
 * found.
 */
public static Node getChildNodeByLocalName(final Node parentNode, final String localNodeName) {
    final NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node child = children.item(i);
        if (child.getLocalName() != null) {
            if (child.getLocalName().equals(localNodeName)) {
                return child;
            }
        }
    }
    return null;
}

From source file:Main.java

public static Element getElementByTagNameNS(Node element, String namespace, String name) {
    NodeList elements = element.getChildNodes();
    CharSequence colon = ":";
    if (elements != null) {
        for (int i = 0; i < elements.getLength(); i++) {
            if (elements.item(i).getNodeType() == Node.ELEMENT_NODE && (elements.item(i).getAttributes()
                    .getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "nil") == null
                    || !"true".equals(elements.item(i).getAttributes()
                            .getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "nil")))) {
                Element currentElement = (Element) elements.item(i);
                String nodeName = currentElement.getNodeName();
                String nodeNameOnly = nodeName;
                if (nodeName.contains(colon)) {
                    String[] nodeNameSplit = nodeName.split(":");
                    nodeNameOnly = nodeNameSplit[1];
                }/*from  www .ja  va2 s .  c  o  m*/

                if ((currentElement.getNamespaceURI() == null
                        || currentElement.getNamespaceURI().equals(namespace)) && nodeNameOnly.equals(name)) {
                    return currentElement;
                }
            }
        }
    }

    return null;
}

From source file:Main.java

public static Object transformXmlNodesIntoMap(NodeList nodes) {
    Map<String, Object> nodeMap = new HashMap<String, Object>();

    if (nodes.getLength() == 1 && nodes.item(0).getNodeType() == Node.TEXT_NODE)
        return nodes.item(0).getTextContent();

    for (int nodeIdx = 0; nodeIdx < nodes.getLength(); nodeIdx++) {
        Node node = nodes.item(nodeIdx);
        NodeList subNodes = node.getChildNodes();

        if (node.getNodeType() != Node.TEXT_NODE)
            nodeMap.put(node.getNodeName(), transformXmlNodesIntoMap(subNodes));
    }/*from  w  w w  .j a v a  2  s . c  o m*/
    return nodeMap;
}

From source file:Main.java

public static List<? extends Node> get_childs(Node parent, String child_name) {
    List<Node> result = new LinkedList<Node>();
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeName().equals(child_name))
            result.add(child);/* ww w  .j  a  va  2 s.co  m*/
    }
    return result;
}

From source file:Main.java

public static int getArg(final Node node, final String strName, final int iDefaultValue) {
    return getArg(node.getChildNodes(), strName, iDefaultValue);
}

From source file:Main.java

/**
 * Extracts a {@code Map} of header tag to value from the header node.
 *
 * @param headerNode the header node/* www  .j  av  a 2 s .  com*/
 * @return a {@code Map} of header tag to value from the header node.
 */
public static Map<String, String> extractHeaderMap(Node headerNode) {
    Map<String, String> headerMap = new HashMap<String, String>();
    for (int i = 0; i < headerNode.getChildNodes().getLength(); i++) {
        Node headerChildNode = headerNode.getChildNodes().item(i);
        headerMap.put(headerChildNode.getLocalName(), headerChildNode.getFirstChild().getNodeValue());
    }
    return headerMap;
}

From source file:Main.java

public static boolean isLeaf(Node node) {

    NodeList nodeList = node.getChildNodes();

    boolean hasElement = false;

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node child = nodeList.item(i);
        if (child instanceof Text) {
            if (!"".equals(child.getTextContent().trim())) {
                // The current node is a leaf node if it contains
                // at least one text node with non-empty text values
                return true;
            }/*from www  .j ava  2 s . c om*/
        }

        if (child instanceof Element) {
            hasElement = true;
        }
    }

    // The current node is also a leaf node if it
    // contains no elements at all
    return !hasElement;
}

From source file:Main.java

/**
 * find elements by name.//from   ww w.j  a  v a  2  s. c  o  m
 * 
 * @param node - current node
 * @param name - name element
 * @param elements - list of found elements
 */
static public void findElementsByName(Node node, List<Node> elements, String name) {
    // get children
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        // if current child is required then add his to list
        if (name.equalsIgnoreCase((child.getNodeName()))) {
            elements.add(child);
        } else {
            findElementsByName(child, elements, name);
        }
    }
}