Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is 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 * * Contributors: * Exadel, Inc. and Red Hat, Inc. - initial API and implementation ******************************************************************************/ import java.util.ArrayList; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; public class Main { /** * Gets all facets of sourceElement. If facet has a few children the method * will return first one. * * @param sourceElement the source element * @param facetName * * param returnTextNode return child text node if facet doesn't have * any child elements; * @param returnTextNode the return text node * * @return the facets */ public static ArrayList<Node> getFacets(Element sourceElement, boolean returnTextNode) { ArrayList<Node> facets = new ArrayList<Node>(); NodeList children = sourceElement.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node instanceof Element && "f:facet".equals(node.getNodeName())) { //$NON-NLS-1$ Element element = (Element) node; NodeList childNodes = element.getChildNodes(); Text textNode = null; for (int j = 0; j < childNodes.getLength(); j++) { Node child = childNodes.item(j); if (child instanceof Element) { facets.add(child); break; } else if (child instanceof Text) { textNode = (Text) child; } } if (returnTextNode && facets.isEmpty()) { facets.add(textNode); } } } return facets; } }