2011-02-17 38 views
0

我有一個XML如下。我想檢索使用公共沼澤地圖對象

<list> 
    <map> 
    <map name="CLAIM_TICKET_1"> 
    <val name="CLAIM_TICKET_Groups">Kalyan</val> 
    <val name="Testing">Ram</val> 
    </map> 
    <map name="CLAIM_TICKET_2"> 
    <val name="CLAIM_TICKET_Groups">Raju</val> 
    <val name="Testing">Varma</val> 
    </map> 
    </list> 

我試圖檢索,作爲使用沼氣地圖對象。

我想輸出爲:

作爲Map對象的列表。

{CLAIM_TICKET_1={CLAIM_TICKET_Groups=Kalyan,Testing=Ram}, 
CLAIM_TICKET_2={CLAIM_TICKET_Groups=Raju,Testing=Varma}} 

我的代碼顯示錯誤:

字符串的Xpath = 「圖/圖」;

digester.addObjectCreate(Xpath, HashMap.class); 
    digester.addCallMethod(Xpath , "put", 2); 
    digester.addCallParam(Xpath, 0, "name");   

    digester.addObjectCreate(Xpath, HashMap.class); 
    digester.addCallMethod(Xpath+"/val" , "put", 2); 
    digester.addCallParam(Xpath+"/val", 0, "name");   
    digester.addCallParam(Xpath+"/val", 1); 

    digester.addSetNext(Xpath+"/val", "put"); 
+0

能否請您縮進您的XML和Java代碼?這會讓閱讀更容易。 – 2011-03-07 08:54:26

回答

0
String Xpath = "map/map"; 
//For every occurence of list/map add map object to list object 
    digester.addObjectCreate("list/map", ArrayList.class); 

    digester.addObjectCreate(Xpath, HashMap.class); 
    digester.addCallMethod(Xpath+"/val" , "put", 2); 
    digester.addCallParam(Xpath+"/val", 0, "name");   
    digester.addCallParam(Xpath+"/val", 1); 

digester.addSetNext("list/map", "add"); 


It works fine. 
相關問題