Java examples for XML:XML Element Child
Get the first XML element with the given name as a child of the given element.
/*//from w w w. ja va 2 s.c om * DOMUtil.java * Copyright (C) 2006 Nicholas Killewald * Portions Copyright (C) 2005 Patrick Niemeyer * * This file is distributed under the terms of the BSD license. * See the LICENSE file under the toplevel images/ directory for terms. */ //package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class Main { /** * Get the first element with the given name as a child of the given * element. A RuntimeException will be thrown if the named element doesn't * exist. * * @param element element to get a child from * @param name name of child * @return the element in question */ public static Element getFirstElement(Element element, String name) { NodeList nl = element.getElementsByTagName(name); if (nl.getLength() < 1) throw new RuntimeException("Element: " + element + "does not contain: " + name); return (Element) nl.item(0); } }