Java tutorial
//package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { public static String getFirstChildElementText(Node node) { return getElementText(getFirstChildElement(node)); } public static String getFirstChildElementText(Node node, String elemName) { return getElementText(getFirstChildElement(node, elemName)); } public static String getElementText(Element ele) { // is there anything to do? if (ele == null) { return null; } // get children text Node child = ele.getFirstChild(); if (child != null) { short type = child.getNodeType(); if (type == Node.TEXT_NODE) { return child.getNodeValue(); } } // return text value return null; } /** Finds and returns the first child element node. */ public static Element getFirstChildElement(Node parent) { if (parent == null) return null; // search for node Node child = parent.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { return (Element) child; } child = child.getNextSibling(); } // not found return null; } /** Finds and returns the first child node with the given name. */ public static Element getFirstChildElement(Node parent, String elemName) { if (parent == null) return null; // search for node Node child = parent.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equals(elemName)) { return (Element) child; } } child = child.getNextSibling(); } // not found return null; } }