Java XML Element Get getElements(Element document, String elementName, String attrName, String attrValue)

Here you can find the source of getElements(Element document, String elementName, String attrName, String attrValue)

Description

Parses the document for a particular element containing the specified attribute

License

Open Source License

Parameter

Parameter Description
document The document to be parsed
elementName The tag name of the element
attrName The name of the attribute
attrValue The value of the attribute

Return

A list containing all the choosen elements.

Declaration

public static List getElements(Element document, String elementName,
        String attrName, String attrValue) 

Method Source Code

//package com.java2s;
/*//w  ww.  jav  a2  s.  c o m
 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). 
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:
 *
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Parses the document for a particular element containing the specified
     * attribute
     * 
     * @param document The document to be parsed
     * @param elementName The tag name of the element
     * @param attrName The name of the attribute
     * @param attrValue The value of the attribute
     * @return A list containing all the choosen elements.
     */
    public static List getElements(Element document, String elementName,
            String attrName, String attrValue) {

        List<Object> choosenElements = new ArrayList<Object>();

        NodeList elementList = document.getElementsByTagName(elementName);

        for (int i = 0; i < elementList.getLength(); i++) {
            Map attr = getAttributes(elementList.item(i));

            String value = (String) attr.get(attrName);

            if (value != null && value.equalsIgnoreCase(attrValue)) {
                choosenElements.add(elementList.item(i));
            }
        }

        return choosenElements;
    }

    /**
     * Fetches the attributes associated with the given node
     * 
     * @param element The element whose attributes need to be fetched
     * @return A map containing all the attributes
     */
    public static Map<Object, Object> getAttributes(Node element) {

        Map<Object, Object> attr = new HashMap<Object, Object>();
        attr.clear();

        NamedNodeMap attributes = element.getAttributes();

        if (attributes != null) { // has attributes
            for (int i = 0; i < attributes.getLength(); i++) {
                Node n = attributes.item(i);
                attr.put(n.getNodeName(), n.getNodeValue());
            }
        }

        return attr;
    }
}

Related

  1. getElementPath(Element elem)
  2. getElementPath(Element element)
  3. getElementPath(Element element)
  4. getElementPosition(Element element)
  5. getElements(Element aElement)
  6. getElements(Element elem, String path)
  7. getElements(Element element)
  8. getElements(Element element, String tagName)
  9. getElements(Element node)