Java tutorial
//package com.java2s; /** * Copyright (c) {2003,2011} {openmobster@gmail.com} {individual contributors as indicated by the @authors tag}. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html */ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class Main { /** * Determines if the specified Parent contains the specified Element * * @param parent * @param elementName * @return true: if the Parent contains the Element, false: otherwise */ public static boolean contains(Element parent, String elementName) { boolean contains = false; NodeList nodes = parent.getElementsByTagName(elementName); if (nodes != null && nodes.getLength() > 0) { contains = true; } return contains; } /** * Determines if the specified Document (DOM Tree) contains the specified Element * * @param document * @param elementName * @return true: if the Document(DOM Tree) contains the Element, false: otherwise */ public static boolean contains(Document document, String elementName) { boolean contains = false; NodeList nodes = document.getElementsByTagName(elementName); if (nodes != null && nodes.getLength() > 0) { contains = true; } return contains; } }