Java XML Element Get findElementList(String name, String attrName, String attrValue, Document doc)

Here you can find the source of findElementList(String name, String attrName, String attrValue, Document doc)

Description

Gets a list of DOM elements with the specified name that have an attribute with the given name and value.

License

Open Source License

Declaration

public static Vector findElementList(String name, String attrName, String attrValue, Document doc) 

Method Source Code


//package com.java2s;

import java.util.Vector;

import org.w3c.dom.*;

public class Main {
    /**//  w w w  . jav  a2s  . co  m
     * Gets a list of DOM elements with the specified name throughout
     * the document (not just children of a specific element).
     */
    public static Vector findElementList(String name, Document doc) {
        return findElementList(name, null, null, doc);
    }

    /**
     * Gets a list of DOM elements with the specified name
     * that have an attribute with the given name and value.
     */
    public static Vector findElementList(String name, String attrName, String attrValue, Document doc) {
        if (name == null)
            return null;
        Vector v = new Vector();
        NodeList list = doc.getElementsByTagName(name);
        int size = list.getLength();
        for (int i = 0; i < size; i++) {
            Node node = list.item(i);
            if (!(node instanceof Element))
                continue;
            Element el = (Element) node;
            if (attrName == null || attrValue == null || attrValue.equals(getAttribute(attrName, el))) {
                v.add(el);
            }
        }
        return v;
    }

    /**
     * Gets the value of the given DOM element's attribute
     * with the specified name.
     */
    public static String getAttribute(String name, Element el) {
        if (name == null || el == null)
            return null;
        if (!el.hasAttribute(name))
            return null;
        return el.getAttribute(name);
    }
}

Related

  1. findElement(Document doc, String elementNS, String elementName, String attrName, String attrValue)
  2. findElement(Document doc, String tagName, Properties props)
  3. findElement(String name, Document doc)
  4. findElementAndSetElseCreateAndSet(Document document, Element parent, String child, boolean value)
  5. findElementElseCreateAndSet(Document document, Element parent, String child, boolean value)
  6. findElementOrContainer(Document document, Element parent, String element)
  7. getElement(Document doc, QName elementQName)
  8. getElement(Document doc, String path)
  9. getElement(Document pDocument, String psTagName, int index)