2012-03-29 43 views
1

我將xml數據以字符串形式存儲到用戶偏好中。我想從這個xml字符串創建hashmap。請幫幫我;這裏是我的xml字符串;來自XML字符串的散列圖 - Android

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> "; 

我想要從數組中的「模塊」標籤中檢索模塊,並且數組必須包含每個模塊的子標籤。請提出一些解決方案。

回答

2

你可以做使用節點列表

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); 
    } 
} 

update XML Dynamically

+0

另一個實施例http://stackoverflow.com/questions/9781568/update-data-in-java-class-as-per-change-in-attribute-in-the-xml-file/9783154# 9783154 – 2012-03-29 07:16:57

+0

謝謝[Riddhish.Chaudhari] [1] [1]:http://stackoverflow.com/users/1236051/riddhish-chaudhari – 2013-05-06 10:02:27

0

如何感謝你的男人的回覆@ Riddhish.Chaudhari。是NodeList是我的要求的實際解決方案,這是我如何解決它..

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

然後我實現了以下功能;

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()); 
    } 
} 

我在我的項目項目中添加了下面的類,它在上面的函數中存儲了來自xml的Role()對象中的數據;

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

現在,無論何時我需要獲取數據,我都可以使用以下方法;

假設我想從獲取元素調用模塊,然後使用散列圖角色;

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;