Java tutorial
//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.Document; import org.w3c.dom.Node; public class Main { /** * Updates the specified the child node * * @param parent the parent node * @param nodeName the name of child node to update * @param nodeValue the new value * @return boolean true * @throws Exception */ public static boolean updateChildNodeValueByName(Document doc, Node parent, String nodeName, String nodeValue) throws Exception { Node node = findChildNodeByName(parent, nodeName); if (node == null) return false; Node firstNode = node.getFirstChild(); if (firstNode == null) { node.appendChild(doc.createTextNode(nodeValue)); } else { firstNode.setNodeValue(nodeValue); } return true; } /** * 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; } }