Java XML Element Get by Name getElement(String name, int index, Element parent)

Here you can find the source of getElement(String name, int index, Element parent)

Description

get Element

License

Open Source License

Declaration

public static Element getElement(String name, int index, Element parent) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

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

public class Main {
    /**// w  ww .  j  a v a2s  . co  m
     */
    public static Element getElement(String name, int index, Element parent) {
        ArrayList<Element> toReturn = getElementsByTag(name, parent);
        if (index > toReturn.size() - 1) {
            return null;
        }
        return toReturn.get(index);
    }

    /**
     */
    public static ArrayList<Element> getElementsByTag(String tag, Element searchIn) {
        NodeList list = searchIn.getElementsByTagName(tag);
        ArrayList<Element> toReturn = new ArrayList<Element>();
        for (int i = 0; i < list.getLength(); i++) {
            Node n = list.item(i);
            if (!(n instanceof Element)) {
                continue;
            }
            toReturn.add((Element) n);
        }
        return toReturn;
    }
}

Related

  1. getElement(Element parent, String tagName)
  2. getElement(Element parent, String tagName)
  3. getElement(Element parentElement, String nodeName)
  4. getElement(Node parent, int index)
  5. getElement(String elementName, Element parentElement)
  6. getElementByName(Document document, String name)
  7. getElementByName(Element eParent, String elementName)
  8. getElementByName(Element parent, String nodeName)
  9. getElementByTag(HTMLDocument htmlDoc, int offset, Tag tag)