Java tutorial
//package com.java2s; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Finds the first element which name matchtes a given tag name * that is locacted anywhere below the given parent. * * @param parent the parent element below which to search the child * @param tagName the (tag) name of the desired child element * @return the child element if an element of that name existed, or null otherwise */ static public Element findFirstChildDeep(Element parent, String tagName) { // Child Element suchen if (parent == null) return null; NodeList nl = parent.getElementsByTagName(tagName); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) return (Element) nl.item(i); } return null; } /** * Returns the first element which name matchtes a given tag name. * * @param doc the xml document in which to find an element of the given name * @param tagName the (tag) name of the desired child element * @return the child element if an element of that name existed, or null otherwise */ static public Element findFirstChildDeep(Document doc, String tagName) { // Child Element suchen if (doc == null) return null; return findFirstChildDeep(doc.getDocumentElement(), tagName); } }