Java XML Element Children getSubelementStrings(final Element element, final String name)

Here you can find the source of getSubelementStrings(final Element element, final String name)

Description

Locate all sub-elements tagged 'name', return their values.

License

Open Source License

Parameter

Parameter Description
element Element where to start looking.
name Name of sub-element to locate.

Return

Returns String array or null if nothing found.

Declaration

public static final String[] getSubelementStrings(final Element element, final String name) 

Method Source Code

//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);
    }
}

Related

  1. getSubelementDouble(final Element element, final String element_name)
  2. getSubelementDouble(final Element element, final String name, final double default_value)
  3. getSubelementString(final Element element, final String name)
  4. getSubelementString(final Element element, final String name)
  5. getSubelementString(final Element element, final String name, final String default_value)
  6. getSubelementText(Element element, String subelementName)
  7. getSubNodeText(Element modsroot, String nodename, String subnodename)
  8. getSubNodeValue(Element element, String name)