Java XML Attribute Load getAllAttributes(Element elem, String name)

Here you can find the source of getAllAttributes(Element elem, String name)

Description

Get the attribute values of a given name/value pair for the first XML org.w3c.dom.Element of given name.

License

Apache License

Parameter

Parameter Description
elem the parent XML Element
name the name of the child text Element

Return

attribute name and value Map of named child Element

Declaration

public static Map<String, String> getAllAttributes(Element elem, String name) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

import java.util.Map;
import java.util.TreeMap;

public class Main {
    /**/*  www.  java  2 s .  c om*/
     * Get the attribute values of a given name/value pair for
     * the first XML {@code org.w3c.dom.Element} of given name.
     *
     * @param elem the parent XML Element
     * @param name the name of the child text Element
     * @return attribute name and value Map of named child Element
     */
    public static Map<String, String> getAllAttributes(Element elem, String name) {
        Map<String, String> attributes = new TreeMap<String, String>();
        NodeList nodeList = elem.getElementsByTagName(name);
        int length = nodeList.getLength();
        for (int n = 0; n < length; ++n) {
            attributes.put(((Element) nodeList.item(n)).getAttribute("name"),
                    ((Element) nodeList.item(n)).getAttribute("value"));
        }
        return attributes;
    }
}

Related

  1. getAllAttributes(Element e)
  2. getAllAttributes(Element element)
  3. getAllAttributes(Node node)
  4. getAllAttributes(Node node)
  5. getAllAttributesAsMap(@Nullable final Element aSrcNode)