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 List<org.w3c.dom.Node> getMatchingChildren(org.w3c.dom.Node node, String name) {
    if (node == null) {
        return null;
    }//from w w w.j a v a 2  s .c  om

    LinkedList<org.w3c.dom.Node> returnList = new LinkedList<org.w3c.dom.Node>();

    org.w3c.dom.NodeList childList = node.getChildNodes();
    int len = childList.getLength();
    for (int i = 0; i < len; i++) {
        org.w3c.dom.Node curNode = childList.item(i);
        if (name.equals(curNode.getNodeName())) {
            returnList.add(curNode);
        }
    }
    return returnList;
}

From source file:com.hiperium.commons.client.soap.AthenticationDispatcherTest.java

/**
 * //from  www .  j  av  a2 s .  co m
 * @param response
 */
public static void readSOAPMessageResponse(SOAPMessage response) {
    SOAPBody authBodyResponse;
    try {
        authBodyResponse = response.getSOAPBody();
        Node authNode = authBodyResponse.getFirstChild();
        NodeList authNodeList = authNode.getChildNodes();
        String responseString = authNodeList.item(0).getFirstChild().getNodeValue();
        LOGGER.debug("RESPONSE: " + responseString);
    } catch (SOAPException e) {
        LOGGER.error(e.getMessage(), e);
    }
}

From source file:eu.planets_project.services.utils.cli.CliMigrationPaths.java

private static CliMigrationPath decodePathNode(Node path) throws URISyntaxException {
    NodeList children = path.getChildNodes();
    Set<URI> froms = null;
    Set<URI> tos = null;
    String command = null;//from w ww. j a  va2 s .  co m
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {

            if (child.getNodeName().equals("from")) {
                froms = decodeFromOrToNode(child);
            }
            if (child.getNodeName().equals("to")) {
                tos = decodeFromOrToNode(child);
            }
            if (child.getNodeName().equals("command")) {
                command = decodeCommandNode(child);
            }

        }
    }
    return new CliMigrationPath(froms, tos, command);

}

From source file:Main.java

/**
 * Extracts child elements with given name from node whith type <code>ELEMENT_NODE</code>.
 *
 * @param node        root node of XML document for search.
 * @param elementName name of elements that should be returned.
 * @return iterator with proper node childs.
 *//*  w  w  w. ja va  2s .com*/
public static Iterator<Element> getChildElements(final Node node, final String elementName) {
    //        node.normalize();
    return new Iterator<Element>() {
        private final NodeList nodes = node.getChildNodes();
        private int nextPos = 0;
        private Element nextElement = seekNext();

        public boolean hasNext() {
            return nextElement != null;
        }

        public Element next() {
            if (nextElement == null)
                throw new NoSuchElementException();
            final Element result = nextElement;
            nextElement = seekNext();
            return result;
        }

        public void remove() {
            throw new UnsupportedOperationException("operation not supported");
        }

        private Element seekNext() {
            for (int i = nextPos, len = nodes.getLength(); i < len; i++) {
                final Node childNode = nodes.item(i);
                if (childNode.getNodeType() == Node.ELEMENT_NODE
                        && childNode.getNodeName().equals(elementName)) {
                    nextPos = i + 1;
                    return (Element) childNode;
                }
            }
            return null;
        }
    };
}

From source file:Main.java

/** Returns the entire value of a node (child nodes and text) as a String
 * @param node/*from   w  w  w. j  a v  a 2 s  .c om*/
 * @return
 */
public static String nodeChildrenToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++)
            t.transform(new DOMSource(nl.item(i)), new StreamResult(sw));
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    return sw.toString().trim();
}

From source file:Main.java

/** Returns the entier value of a node (child nodes and text) as a String
 * @param node// w  ww .ja  v  a2s .c  om
 * @return
 */
public static String nodeChildrenToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++)
            t.transform(new DOMSource(nl.item(i)), new StreamResult(sw));
        //      dimes.AgentGuiComm.GUICommunicator.sendLog(Level.WARNING, "", sw.toString());
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    String test = sw.toString();
    return sw.toString().trim();
}

From source file:Main.java

/**
 * This method returns node value for given child. If there is no text
 * available for given node, then this method returns null
 * /*  w ww . ja va 2s  . c  o  m*/
 * @return Node value of input node
 * @throws IllegalArgumentException
 *             if input is invalid
 */
public static String getNodeValue(final Node inputNode) {
    // Child count
    int childCount = 0;

    if (inputNode == null) {
        return null;
    }

    // Return null if child not found
    final NodeList childList = inputNode.getChildNodes();
    if ((childList == null) || (childList.getLength() < 1)) {
        return null;
    }

    // Get child count
    childCount = childList.getLength();

    // For each child
    for (int childIndex = 0; childIndex < childCount; childIndex++) {
        // Get each child
        final Node childNode = childList.item(childIndex);

        // Check if text node
        if (childNode.getNodeType() == Node.TEXT_NODE) {
            // Return node value
            return childNode.getNodeValue();
        }
    }

    // If no text node found return null
    return null;
}

From source file:Main.java

public static void removeElementXML(Node node, short nodeType, String name) {
    if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
        node.getParentNode().removeChild(node);
    } else {/*from  ww w.  j  a  v a 2  s .c  o  m*/
        // Visit the children
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeElementXML(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

/**
 *  Sets the text for a node/*from  w  w  w .ja v  a2 s  .c om*/
 */
public synchronized static void setText(Node n, String text) {
    if (n == null)
        throw new IllegalArgumentException("Node argument cannot be null");
    if (text == null)
        throw new IllegalArgumentException("Node text argument cannot be null");

    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
            nl.item(i).setNodeValue(text);
            return;
        }
    }

    Node textNode = n.getOwnerDocument().createTextNode(text);
    n.appendChild(textNode);
}

From source file:com.threewks.thundr.googleapis.cloudstorage.GoogleCloudStorageServiceImpl.java

static List<Node> findNamedChildren(Node node, String name) {
    List<Node> children = new ArrayList<Node>();
    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (name.equalsIgnoreCase(child.getNodeName())) {
            children.add(child);//  www. j a v a  2  s. co  m
        }
    }
    return children;
}