Java XML Attribute Read readNodeAttributes(Element elem, Object[] map)

Here you can find the source of readNodeAttributes(Element elem, Object[] map)

Description

Read the attributes of elem.

License

GNU General Public License

Parameter

Parameter Description
elem the element to analyze
map the map containing the attributes type.

Declaration

public static void readNodeAttributes(Element elem, Object[] map) 

Method Source Code

//package com.java2s;
/*/* w w w.  j  a va2 s . c om*/
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2010 - Calixte DENIZET
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 */

import java.util.Map;

import java.util.StringTokenizer;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Read the attributes of elem.
     * Map must be like this: "attr1" -> integer.class, "attr2" -> boolean.class, ...
     * Map will be filled with the value (as Object) of the different attributes
     * @param elem the element to analyze
     * @param map the map containing the attributes type.
     */
    public static void readNodeAttributes(Element elem,
            Map<String, Object> map) {
        NamedNodeMap attrMap = elem.getAttributes();
        for (int i = 0; i < attrMap.getLength(); i++) {
            Attr attr = (Attr) attrMap.item(i);
            String key = attr.getName();
            if (map.containsKey(key)) {
                map.put(key, convert(attr.getValue(), (Class) map.get(key)));
            }
        }
    }

    /**
     * Read the attributes of elem.
     * Map must be like this: Object[]{"attr1", integer.class, "attr2", boolean.class, ...}
     * Map will be filled with the value (as Object) of the different attributes
     * @param elem the element to analyze
     * @param map the map containing the attributes type.
     */
    public static void readNodeAttributes(Element elem, Object[] map) {
        NamedNodeMap attrMap = elem.getAttributes();
        for (int i = 0; i < attrMap.getLength(); i++) {
            Attr attr = (Attr) attrMap.item(i);
            String key = attr.getName();
            for (int j = 0; j < map.length; j += 2) {
                if (map[j].equals(key)) {
                    map[j + 1] = convert(attr.getValue(),
                            (Class) map[j + 1]);
                }
            }
        }
    }

    /**
     * Read the attributes of first element named nodeName in the document.
     * Map must be like this: "attr1" -&gt; integer.class, "attr2" -&gt; boolean.class, ...
     * Map will be filled with the value (as Object) of the different attributes
     * @param doc the document
     * @param nodeName the node name
     * @param map the map containing the attributes type.
     * @return the corresponding element or null if it doesn't exist
     */
    public static Element readNodeAttributes(Document doc, String nodeName,
            Map<String, Object> map) {
        NodeList list = doc.getDocumentElement().getElementsByTagName(
                nodeName);
        if (getNodeListLength(list) > 0) {
            Node n = list.item(0);
            if (n instanceof Element) {
                readNodeAttributes((Element) n, map);
                return (Element) n;
            }
        }

        return null;
    }

    /**
     * Read the attributes of first element named nodeName in the document.
     * Map must be like this: Object[]{"attr1", integer.class, "attr2", boolean.class, ...}
     * Map will be filled with the value (as Object) of the different attributes
     * @param doc the document
     * @param nodeName the node name
     * @param map the map containing the attributes type.
     * @return the corresponding element or null if it doesn't exist
     */
    public static Element readNodeAttributes(Document doc, String nodeName,
            Object[] map) {
        NodeList list = doc.getDocumentElement().getElementsByTagName(
                nodeName);
        if (getNodeListLength(list) > 0) {
            Node n = list.item(0);
            if (n instanceof Element) {
                readNodeAttributes((Element) n, map);
                return (Element) n;
            }
        }

        return null;
    }

    /**
     * Convert a value (as String) into an object according to its class type giving in clazz
     * @param value the value to convert
     * @param clazz the class type of the value
     * @return the converted object
     */
    private static final Object convert(String value, Class clazz) {
        if (clazz == int.class) {
            return Integer.parseInt(value);
        } else if (clazz == float.class) {
            return Float.parseFloat(value);
        } else if (clazz == boolean.class) {
            return Boolean.parseBoolean(value);
        } else if (clazz == double.class) {
            return Double.parseDouble(value);
        } else if (clazz == short.class) {
            return Short.parseShort(value);
        } else if (clazz == long.class) {
            return Long.parseLong(value);
        } else if (clazz == byte.class) {
            return Byte.parseByte(value);
        } else if (clazz == char.class) {
            return value.length() == 0 ? '\0' : value.charAt(0);
        } else if (clazz == String[].class) {
            return getArray(value, ";");
        }

        return value;
    }

    /**
     * @param list a node list
     * @return the length
     */
    private static int getNodeListLength(NodeList list) {
        int length = 0;
        try {
            length = list.getLength();
        } catch (NullPointerException e) {
            /* Avoid Java bug */
        }
        return length;
    }

    /**
     * Get an array from a string containing strings separated with a delimitor
     * @param value the string to parse
     * @param delimitor the delimitor
     * @return an array of String
     */
    private static final String[] getArray(String value, String delimitor) {
        StringTokenizer tokens = new StringTokenizer(value, delimitor);
        String[] arr = new String[tokens.countTokens()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = tokens.nextToken();
        }

        return arr;
    }
}

Related

  1. readFloat(Node node, String attributeName, float def)
  2. readFloatAttribute(XMLStreamReader reader, String attributeName)
  3. readInt(Node node, String attributeName, int def)
  4. readIntAttr(Element element, String attributeName, int defaultValue)
  5. readIntegerAttribute(Element elem, String name, int defaultValue)