Rizwan Sohaib
Rizwan Sohaib

Reputation: 1260

Hashmap from XML String - Android

I am storing xml data in a string into user preferences. I want to create hashmap from this xml string. please help me; here is my xml string;

    String data = " <modules> <Calls> <access>89</access> <delete>90</delete> <edit>90</edit> <export>90</export> <import>90</import> <list>90</list> <view>90</view> </Calls> <Cases> <access>89</access> <delete>90</delete> <edit>90</edit> <export>90</export> <import>90</import> <list>90</list> <view>90</view> </Cases> <CFM_CoBuyer> <access>89</access> <delete>90</delete> <edit>90</edit> <export>90</export> <import>90</import> <list>90</list> <view>90</view> </CFM_CoBuyer> <Contacts> <access>89</access> <delete>-99</delete> <edit>90</edit> <export>90</export> <import>90</import> <list>90</list> <view>90</view> </Contacts> <Inv_Inventory> <access>89</access> <delete>90</delete> <edit>90</edit> <export>90</export> <import>90</import> <list>90</list> <view>90</view> </Inv_Inventory> <Meetings> <access>89</access> <delete>90</delete> <edit>90</edit> <export>90</export> <import>90</import> <list>90</list> <view>90</view> </Meetings> <Notes> <access>89</access> <delete>90</delete> <edit>90</edit> <export>90</export> <import>90</import> <list>90</list> <view>90</view> </Notes> <Opportunities> <access>89</access> <delete>-99</delete> <edit>90</edit> <export>-99</export> <import>-99</import> <list>80</list> <view>90</view> </Opportunities> <Tasks> <access>89</access> <delete>90</delete> <edit>90</edit> <export>90</export> <import>90</import> <list>90</list> <view>90</view> </Tasks> <Trd_TradeIns> <access>89</access> <delete>90</delete> <edit>90</edit> <export>90</export> <import>90</import> <list>90</list> <view>90</view> </Trd_TradeIns> </modules> ";

I want to retrieve modules from "modules" tag in an array, and the array must contain sub tags for each module. Please suggest some solution.

Upvotes: 1

Views: 2028

Answers (2)

Rizwan Sohaib
Rizwan Sohaib

Reputation: 1260

Thank you man for the reply @Riddhish.Chaudhari. Yes NodeList was the actual solution to my requirement, Here is how I solved it..

public static HashMap<String, Role> roles = null;
public static String user_roles = null;

then I implemented following function;

public static void parseRoles(){
    roles = new HashMap<String, Role>();
    user_roles = data; // my xml string

    try {
        Document dom = getXMLDocument(user_roles);

        NodeList modules = dom.getDocumentElement().getChildNodes();

        for (int i=0; i<modules.getLength(); i++){
            Role role = new Role();

            Node module = modules.item(i);
            String moduleName = module.getNodeName();

            NodeList accesses = module.getChildNodes();

            for (int j=0; j<accesses.getLength(); j++){
                Node access = accesses.item(j);

                String name = access.getNodeName();
                int accessValue = -1;
                try{
                    accessValue = Integer.parseInt(access.getFirstChild().getNodeValue().trim());
                }catch (Exception e) {
                    AlertHelper.logError(e.getClass().getCanonicalName(), e.getMessage());
                }

                if(accessValue != -1){
                    if(name.equalsIgnoreCase("access")){
                        role.access = accessValue;
                    }else if(name.equalsIgnoreCase("view")){
                        role.view = accessValue;
                    }else if(name.equalsIgnoreCase("list")){
                        role.list = accessValue;
                    }else if(name.equalsIgnoreCase("edit")){
                        role.edit = accessValue;
                    }else if(name.equalsIgnoreCase("delete")){
                        role.delete = accessValue;
                    }
                }
            }

            roles.put(moduleName.toLowerCase(), role);
        }
    } catch (Exception e) {
       roles = null;
       AlertHelper.logError(e.getClass().getCanonicalName(), e.getMessage());
    } 
}

I added following class in my project project, which is storing data from xml in Role() object in the above function;

public class Role {
    public int access = 89;
    public int view = 90;
    public int list = 90;
    public int edit = 90;
    public int delete = 90;
}

Now whenever I need to get data I can use following way;

Suppose I want to get elements from Calls module, then using hashmap roles;

int access = roles.get("Calls").access;
int view = roles.get("Calls").view;
int list = roles.get("Calls").list;
int edit = roles.get("Calls").edit;
int delete = roles.get("Calls").delete;

Upvotes: 0

Riddhish.Chaudhari
Riddhish.Chaudhari

Reputation: 853

you can do using node List

Document document = null;
NodeList nodeList = null;
Node node = null;

nodeList = document.getElementsByTagName("modules").item(0).getChildNodes();
HashMap <String,Object> localParameterMap  = new HashMap<String,Object>();

for(int i=0; i<nodeList.getLength(); i++){
    node = nodeList.item(i);
    if(node.getNodeName().equals("Calls")){
        Collection objCollection = readAttributeList(node);
        localParameterMap.put(ATTRIBUTE_LIST, objCollection);
    }
}

How to update XML Dynamically

Upvotes: 2

Related Questions