Android XML Attribute Get extractAttributesFromTag(String tag)

Here you can find the source of extractAttributesFromTag(String tag)

Description

Extracts all attributes from tags, for example

Parameter

Parameter Description
tag a parameter

Return

null if nothing found, with attributeName-attributeValue pairs.

Declaration

public static Map<String, String> extractAttributesFromTag(String tag) 

Method Source Code

//package com.java2s;

import java.util.HashMap;

import java.util.Map;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**/*from  w w  w .  ja  v a  2s .  c o m*/
     * Extracts all attributes from tags, for example <tag attr1="value1" attr2="value2">
     * @param tag
     * @return null if nothing found, {@link HashMap} with attributeName-attributeValue pairs.
     */
    public static Map<String, String> extractAttributesFromTag(String tag) {
        if (tag == null) {
            return null;
        }

        /*
         * extract attribute=value pairs from tag 
         */
        Matcher m = getMatcher(tag, " .*(?=[>])");

        if (m.find() == false) {
            return null;
        }

        Map<String, String> attributes = new HashMap<String, String>();

        String match = m.group();
        if (match == null) {
            return null;
        }

        int attrStart = 0;
        /*
         * Match attribute keys (between ' ' and '=' characters). 
         */
        Matcher mAttrName = getMatcher(match, "(?<= ).*?(?==)");
        while (mAttrName.find(attrStart)) {
            String attrName = mAttrName.group().trim();
            String attrValue = null;

            /*
             * Look for attribute value after the found attribute key.
             */
            int indexForValueStart = mAttrName.end();

            /*
             * Match attribute values (between '="' (equals followed by quote) and '"' (quote)).  
             */
            Matcher mAttrValue = getMatcher(match, "(?<=\").*?(?=\")");
            if (mAttrValue.find(indexForValueStart)) {
                attrValue = mAttrValue.group();

                /*
                 * Set the search for next attribute key-value pair after this (last found) attribute value.
                 */
                attrStart = mAttrValue.end();

                /*
                 * Save the found attribute to list.
                 */
                attributes.put(attrName, attrValue);

                continue;
            }

            /** SHOULD NOT REACH HERE! **/
            break;
        }

        return attributes;
    }

    public static Matcher getMatcher(String xml, String regex) {
        if (xml == null || regex == null) {
            return null;
        }

        Pattern p = Pattern.compile(regex, Pattern.DOTALL
                | Pattern.MULTILINE);
        Matcher m = p.matcher(xml);
        return m;
    }
}

Related

  1. getAttribute(String key, Attributes attributes, Map tagCache)
  2. getAttributesValue(Attributes attrs, String name)
  3. getXmlAttribute(Context context, XmlResourceParser xml, String name)
  4. getNodeAttributeOrDefault(Node node, String name, String defaultValue)