Java tutorial
//package com.java2s; /*--------------------------------------------------------------- * Copyright 2005 by the Radiological Society of North America * * This source software is released under the terms of the * RSNA Public License (http://mirc.rsna.org/rsnapubliclicense) *----------------------------------------------------------------*/ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** * Get the first child element with a specified name. * If the starting node is a Document, use the document element * as the starting point. Only first-generation children of the * starting node are searched. * @param node the starting node. * @param name the name of the child to find. * @return the first child element with the specified name, or null * if the starting node is null or if no child with the name exists. */ public static Element getFirstNamedChild(Node node, String name) { if (node == null) return null; if (node instanceof Document) node = ((Document) node).getDocumentElement(); if (!(node instanceof Element)) return null; Node child = node.getFirstChild(); while (child != null) { if ((child instanceof Element) && child.getNodeName().equals(name)) { return (Element) child; } child = child.getNextSibling(); } return null; } }