Java XML Child Node Get getChildNodeValue(String childNodeName, Node n)

Here you can find the source of getChildNodeValue(String childNodeName, Node n)

Description

searches for a child of n called childNodeName.

License

Open Source License

Parameter

Parameter Description
childNodeName the child node to search for
n the node to search through.

Declaration

public static String getChildNodeValue(String childNodeName, Node n) 

Method Source Code

//package com.java2s;
/*/*from  w w w  . j a  v  a2  s .c o  m*/
 * Copyright (c) 2003-2010 The Regents of the University of California.
 * All rights reserved.
 *
 * '$Author: crawl $'
 * '$Date: 2013-02-21 11:20:05 -0800 (Thu, 21 Feb 2013) $' 
 * '$Revision: 31474 $'
 * 
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the above
 * copyright notice and the following two paragraphs appear in all copies
 * of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 * ENHANCEMENTS, OR MODIFICATIONS.
 *
 */

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * searches for a child of n called childNodeName. If it is found, the
     * nodeValue() of the child is returned. If it is not found, null is
     * returned. See the Xerces documentation for class Node for return values.
     * 
     * @param childNodeName
     *            the child node to search for
     * @param n
     *            the node to search through.
     */
    public static String getChildNodeValue(String childNodeName, Node n) {
        Node child = getChildNodeWithName(childNodeName, n);
        return child.getNodeValue();
    }

    /**
     * looks through the child nodes of n and returns the first child with name
     * 
     * @param name
     *            the name of the child node to search for
     * @param n
     *            the node whose children we are searching
     * @return the found node or null if the node is not found.
     */
    public static Node getChildNodeWithName(String name, Node n) {
        NodeList nl = n.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node c = nl.item(i);
            if (c.getNodeName().equals(name)) {
                return c;
            }
        }
        return null;
    }
}

Related

  1. getChildNodeTextContents(Node node, String name)
  2. getChildNodeValue(Element node, String tagName)
  3. getChildNodeValue(Element root, String childNodeName, String defaultValue)
  4. getChildNodeValue(Node node, String elementName)
  5. getChildNodeValue(Node root, String childName, boolean emptyStringOnMissingChild)
  6. getChildNodeValueByName(Node parent, String nodeName)
  7. getChildNodeWithName(Node node, String name, String namespaceURI)
  8. getChildNodeWithName(String name, Node n)
  9. getChildNodeWithTag(Node node, String nodeTag)