Java tutorial
//package com.java2s; /* * XMLUtils.java * * Copyright (c) 1998 - 2006 BusinessTechnology, Ltd. * All rights reserved * * This program is the proprietary and confidential information * of BusinessTechnology, Ltd. and may be used and disclosed only * as authorized in a license agreement authorizing and * controlling such use and disclosure * * Millennium ERP system. * */ import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** * Return the first child Element with the given name; if name is null returns the first element. */ public static Element firstChildElement(Element element, String childElementName) { if (element == null) return null; // get the first element with the given name Node node = element.getFirstChild(); if (node != null) { do { if (node.getNodeType() == Node.ELEMENT_NODE && (childElementName == null || childElementName.equals(node.getNodeName()))) { Element childElement = (Element) node; return childElement; } } while ((node = node.getNextSibling()) != null); } return null; } /** * Return the first child Element with the given name; if name is null returns the first element. */ public static Element firstChildElement(Element element, String childElementName, String attrName, String attrValue) { if (element == null) return null; // get the first element with the given name Node node = element.getFirstChild(); if (node != null) { do { if (node.getNodeType() == Node.ELEMENT_NODE && (childElementName == null || childElementName.equals(node.getNodeName()))) { Element childElement = (Element) node; String value = childElement.getAttribute(attrName); if (value != null && value.equals(attrValue)) { return childElement; } } } while ((node = node.getNextSibling()) != null); } return null; } }