Here you can find the source of getChildNodeValueByName(Node parent, String nodeName)
Parameter | Description |
---|---|
parent | the parent node |
nodeName | the node name |
Parameter | Description |
---|---|
Exception | an exception |
public static String getChildNodeValueByName(Node parent, String nodeName) throws Exception
//package com.java2s; /******************************************************************************* * Copyright (c) 2015 CA. All rights reserved. * * This source file is licensed under the terms of the Eclipse Public License 1.0 * For the full text of the EPL please see https://www.eclipse.org/legal/epl-v10.html *******************************************************************************/ import org.w3c.dom.Node; public class Main { /**/*from w w w.j a v a 2 s. c o m*/ * Gets a node value by name * * @param parent the parent node * @param nodeName the node name * @return String the value of the node * @throws Exception */ public static String getChildNodeValueByName(Node parent, String nodeName) throws Exception { Node node = findChildNodeByName(parent, nodeName); if (node != null && node.getFirstChild() != null) { return node.getFirstChild().getNodeValue().trim(); } return ""; } /** * Search a child node by name * * @param parent the parent node * @param nodeName the node name for searching * @return Node with the specified name * @see Node * @throws Exception */ public static Node findChildNodeByName(Node parent, String nodeName) throws Exception { Node child = null; Node node = parent.getFirstChild(); while (node != null) { if (node.getNodeName().equals(nodeName)) { child = node; break; } node = node.getNextSibling(); } return child; } }