Java XML Attribute Get getAttributes(Element el)

Here you can find the source of getAttributes(Element el)

Description

Returns a list of attributes of an element.

License

Apache License

Parameter

Parameter Description
el Element whose attributes are returned

Return

the List of Attr

Declaration

static public List getAttributes(Element el) 

Method Source Code

//package com.java2s;
/**/*from w w  w .ja  va  2 s . co m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

import java.util.List;

import java.util.Vector;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

public class Main {
    private static final String ATTR_XMLNS = "xmlns";

    /**
     * Returns a list of attributes of an element. Returns an 
     * empty list if the element has no attributes. Does not
     * include namespace declarations.
     *
     * @param el       Element whose attributes are returned
     * @return the List of Attr
     */
    static public List getAttributes(Element el) {
        String nodename, prefix = null;
        List attrs = new Vector();
        NamedNodeMap attrMap = el.getAttributes();
        for (int i = 0; i < attrMap.getLength(); i++) {
            nodename = attrMap.item(i).getNodeName();
            prefix = attrMap.item(i).getPrefix();

            if (ATTR_XMLNS.equals(nodename) || ATTR_XMLNS.equals(prefix)) {
                //ignore namespace declarations
                continue;
            } else {
                attrs.add(attrMap.item(i));
            }
        }

        return attrs;
    }
}

Related

  1. getAttributeNSName(Element e, String attrName)
  2. getAttributeNSOrNull(Element e, String name, String nsURI)
  3. getAttributeNSOrNull(Element e, String name, String nsURI)
  4. getAttributeOrNull(String attribute, Element element)
  5. getAttributePropertyFromElement(final Element element, final String attribute)
  6. getAttributes(Element el)
  7. getAttributes(Element el)
  8. getAttributes(Element element)
  9. getAttributes(Element element)