2012-06-10 52 views
1

建模我想解析下面的XML(在我的本地機器託管後)並加載它來存儲。是閱讀嵌套的XML與hasMany關聯,Sencha,ExtJS

<?xml version="1.0" encoding="UTF-8"?> 
<users> 
    <user> 
     <id>2</id> 
     <name>shameel</name> 
     <post> 
     <user_id>5</user_id> 
     <title>programmer</title> 
     <body>nothing</body> 
     </post> 
     <post> 
     <user_id>6</user_id> 
     <title>newpost</title> 
     <body>congrats</body> 
     </post> 
    </user> 
<user> 
     <id>3</id> 
     <name>abdulls</name> 
     <post> 
     <user_id>5</user_id> 
     <title>programmer1</title> 
     <body>nothing1</body> 
     </post> 
     <post> 
     <user_id>6</user_id> 
     <title>newpost1</title> 
     <body>congrats1</body> 
     </post> 
     <post> 
     <user_id>7</user_id> 
     <title>newpost1</title> 
     <body>congrats1</body> 
     </post> 
    </user>  
</users> 

使用的型號如下:使用

Ext.define("SectionModel", { 
      extend : 'Ext.data.Model', 
      config : { 
       fields : [{ 
          name : 'section', 
          type : 'string', 
          mapping : '@section' 
         }], 
       proxy : { 
        type : 'ajax', 

        url : 'http://localhost:8080/sample1.xml', //sample1.xml is the file 

        reader : { 
         type : 'xml', 
         rootProperty : 'configs', 
         record : 'navigation' 
        } 
       }, 

       hasMany : [{ 
        model : 'ArticlesModel', 
        name : 'articlesModel', 
         associationKey:'sections' 

        }] 



      } 
     }); 










Ext.define("ArticlesModel", { 
      extend : 'Ext.data.Model', 
      config : { 
       fields : [{ 
         name : 'title', 
         type : 'string', 
         mapping : '@title' 
        }, { 
         name : 'value', 
         type : 'string', 
         mapping : '@value' 
        }], 

        proxy : { 
        type : 'ajax', 
             url : 'http://localhost:8080/sample1.xml', //sample1.xml is the file 
        reader : { 
         type : 'xml', 
         rootProperty : 'navigation', 
         record : 'section' 
        } 
       }, 

       belongsTo : 'SectionModel' 


      } 
     }); 

商店如下:

Ext.define("SectionStore", { 
      extend : 'Ext.data.Store', 

      config : { 
       model : 'SectionModel', 
       autoLoad : 'true' 



      } 
     }); 

我嘗試訪問如下內容:

var store = Ext.data.StoreManager.get('SectionStore'); 
     if (!store) { 
      console.log("Store not found"); 
      return; 
     } 

store.load(function(records, operation, success) { 
        for (var i = 0; i < store.getCount(); i++) { 
         var section_title = store.getAt(i).get('section'); 
               var subsection_val = store.getAt(i).articlesModel().get('title');//this function fails saying no "get" function for the object 
} 
}); 

這裏我能夠獲得section_title。但不能得到subsection_val。我已經試過這與商店中的代理,但無法修復它。任何人都可以幫忙。

回答

0

我遇到了類似的問題,解析嵌套的XML元素。不幸的是,經過對XML解析代碼的大量攻擊之後,我最終放棄了。 (最後寫一個直通到我們的網絡服務,將改變響應中JSON)

您可以瞭解我的(MIS)-adventures這裏:

http://www.sencha.com/forum/showthread.php?189537-Sencha-Touch-2.0-nested-XML-parsing

+0

是JSON是選項。 XML一些如何創建問題。我們將XML封裝在一個jsonp中並獲得它。 – buddy