Java XML Attribute Get getAttributes(Node node)

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

Description

Get all attributes as a Map from a node

License

Open Source License

Parameter

Parameter Description
node the node taht contains the attributes to read

Return

the values or a empty map

Declaration

public static HashMap<String, String> getAttributes(Node node) 

Method Source Code


//package com.java2s;
/*//from  w  ww .  j  a v a2  s .  co m
 * Copyright (c) 2015, Lars Brandt. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

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

import java.util.HashMap;

public class Main {
    /**
     * Find all attribute in a node with specific prefix.
     *
     * @param node         the note that contains the attributes
     * @param prefix       the prefix to search for
     * @param removePrefix true: removes the prefix from attribute name (name is the key of resulting map)
     * @return the values or a empty map
     */
    public static HashMap<String, String> getAttributes(Node node, String prefix, boolean removePrefix) {
        final HashMap<String, String> result = new HashMap<>();

        final NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            final Node att = attributes.item(i);
            String name = att.getNodeName();
            if (name.startsWith(prefix)) {
                if (removePrefix) {
                    name = name.substring(prefix.length());
                }
                result.put(name, att.getNodeValue());
            }
        }
        return result;
    }

    /**
     * Get all attributes as a Map from a node
     *
     * @param node the node taht contains the attributes to read
     * @return the values or a empty map
     */
    public static HashMap<String, String> getAttributes(Node node) {
        final HashMap<String, String> result = new HashMap<>();

        final NamedNodeMap atts = node.getAttributes();
        for (int i = 0; i < atts.getLength(); i++) {
            final Node att = atts.item(i);
            result.put(att.getNodeName(), att.getNodeValue());
        }
        return result;
    }
}

Related

  1. getAttributes(Node node)
  2. getAttributes(Node node)
  3. getAttributes(Node node)
  4. getAttributes(Node node)
  5. getAttributes(Node node)
  6. getAttributes(Node node)
  7. getAttributes(Node node)
  8. getAttributes(Node node)
  9. getAttributes(String element, String xsd)