Java XML Attribute Get getAttributes(Node element)

Here you can find the source of getAttributes(Node element)

Description

Fetches the attributes associated with the given node

License

Open Source License

Parameter

Parameter Description
element The element whose attributes need to be fetched

Return

A map containing all the attributes

Declaration

public static Map<Object, Object> getAttributes(Node element) 

Method Source Code

//package com.java2s;
/*//from  w  w w .j a  v  a2s .  co 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.HashMap;

import java.util.Map;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /**
     * 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. getAttributes(Element element)
  2. getAttributes(Element root, String elementName, String[] wantedAttributes)
  3. getAttributes(final Element el)
  4. getAttributes(final Element element)
  5. getAttributes(final Node node, final String[] attributeNames)
  6. getAttributes(Node n)
  7. getAttributes(Node node)
  8. getAttributes(Node node)
  9. getAttributes(Node node)