Java tutorial
//package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * This method takes Element object and String tagName as argument and * returns the value of the first child node it gets. If it does not find * any node it will return null. * * @param element * Element object * @param tagName * Name of the tag. * @return the value of the first occurance of <code>tagName</code> tag in * <code>element</code> object. Returns null, if doesn't find any. */ public static String getTagValue(Element element, String tagName) { if (element == null) { return null; } NodeList nodes = element.getElementsByTagName(tagName); if (nodes == null || nodes.getLength() <= 0) { return null; } return getTagValue(nodes.item(0)); } /** * This method takes Node object as argument and return the value of the * first child of the node. * <p/> * If there is no such node, this will return null. * * @param node * Node object * @return <code>String</code> returns the value of the node */ public static String getTagValue(Node node) { NodeList childNodeList = node.getChildNodes(); String value; if (childNodeList == null) { value = ""; } else { StringBuffer buffer = new StringBuffer(); for (int j = 0; j < childNodeList.getLength(); j++) { buffer.append(childNodeList.item(j).getNodeValue()); } value = buffer.toString(); } return value; } }