Java XML Element Get by Name getElementsByTagName(Element parent, String name, boolean localOnly)

Here you can find the source of getElementsByTagName(Element parent, String name, boolean localOnly)

Description

get Elements By Tag Name

License

Open Source License

Declaration

public static List<Element> getElementsByTagName(Element parent, String name, boolean localOnly) 

Method Source Code

//package com.java2s;
/*--------------------------------------------------------------------------
 * Copyright (c) 2004, 2006 OpenMethods, LLC
 * 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
 *
 * Contributors:/*  w  ww .j  a v a 2  s .  co  m*/
 *    Trip Gilman (OpenMethods), Lonnie G. Pryor (OpenMethods),
 *    Vincent Pruitt (OpenMethods)
 *    
 *    T.D. Barnes (OpenMethods) - initial API and implementation
 -------------------------------------------------------------------------*/

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static List<Element> getElementsByTagName(Element parent, String name, boolean localOnly) {
        List<Element> ret = new ArrayList<Element>();
        if (!localOnly) {
            NodeList elementList = parent.getElementsByTagName(name);
            for (int i = 0; i < elementList.getLength(); i++) {
                ret.add((Element) elementList.item(i));
            }
        } else {
            NodeList childList = parent.getChildNodes();
            for (int i = 0; i < childList.getLength(); i++) {
                if (childList.item(i).getNodeType() != Node.ELEMENT_NODE)
                    continue;
                Element child = (Element) childList.item(i);
                if (child.getTagName().equals(name))
                    ret.add(child);
            }
        }
        return ret;
    }
}

Related

  1. getElements(Element parent, String tagName)
  2. getElements(Element parentElement, String nodeName)
  3. getElements(final Element parent, final String name)
  4. getElementsByName(Element parent, String name)
  5. getElementsByTagName(Document doc, String tagName)
  6. getElementsByTagName(Element parent, String tagName)
  7. getElementsByTagName(final Element parentElement, final String elementName)
  8. getElementsByTagNameCaseInsensitive( Document doc, final Set lowerCaseNames)
  9. getElementValue(Element parent)