Here you can find the source of getSubelementStrings(final Element element, final String name)
Parameter | Description |
---|---|
element | Element where to start looking. |
name | Name of sub-element to locate. |
public static final String[] getSubelementStrings(final Element element, final String name)
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 Oak Ridge National Laboratory. * 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 java.util.ArrayList; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** Locate all sub-elements tagged 'name', return their values. *// w w w.j a va 2 s. c om * Will only go one level down, not search the whole tree. * * @param element Element where to start looking. * @param name Name of sub-element to locate. * * @return Returns String array or null if nothing found. */ public static final String[] getSubelementStrings(final Element element, final String name) { ArrayList<String> values = null; if (element != null) { Element n = findFirstElementNode(element.getFirstChild(), name); while (n != null) { if (values == null) values = new ArrayList<String>(); values.add(n.getFirstChild().getNodeValue()); n = findNextElementNode(n, name); } } if (values != null && values.size() > 0) { String[] val_array = new String[values.size()]; values.toArray(val_array); return val_array; } return null; } /** Look for Element node if given name. * <p> * Checks the node and its siblings. * Does not descent down the 'child' links. * @param node Node where to start. * @param name Name of the nodes to look for. * @return Returns node or the next matching sibling or null. */ public static final Element findFirstElementNode(Node node, final String name) { while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) return (Element) node; node = node.getNextSibling(); } return null; } /** @return Returns the next matching element. * @see #findFirstElementNode(Node, String) */ public static final Element findNextElementNode(final Node node, final String name) { return findFirstElementNode(node.getNextSibling(), name); } }