Android examples for XML:XML Child Element
Returns the text of the specified direct child of the specified XML element.
//package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /**// w ww .j av a 2 s .com * Returns the text of the specified direct child of the specified element. */ public static String directChildText(Element parent, String childName) { final Element child = directChild(parent, childName); if (child == null) { return null; } return child.getTextContent(); } /** * Returns the direct child node of the specified element having specified name. */ public static Element directChild(Element parent, String childName) { final NodeList nodeList = parent.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { final Node node = nodeList.item(i); if (node instanceof Element) { final Element element = (Element) node; if (element.getTagName().equals(childName)) { return element; } } } return null; } }