读取嵌套的XML以使用hasMany关联,Sencha,ExtJS进行建模 [英] Read nested XML to model with hasMany association, Sencha, ExtJS
问题描述
我正在尝试解析以下xml(在本地计算机中托管之后)并将其加载到存储中.
I am trying to parse the following xml (after hosting in my local machine) and load it to store.
<?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'
}
});
我尝试按以下方式访问内容:
I try to access the content as below:
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.我已经在商店中使用代理尝试了此操作,但无法修复它.谁能帮忙吗?
Here I am able to obtain section_title. But cannot get subsection_val. I ve tried this with the proxy in store, but could not fix it. Can any one please help.
推荐答案
我在解析嵌套的XML元素时遇到了类似的问题.不幸的是,经过对XML解析代码的大量修改之后,我最终放弃了. (最终在我们的Web服务中编写了一个传递,将响应转换为JSON)
I ran into similar issues with parsing nested XML elements. Unfortunately, after much hacking around in the XML Parsing code, I eventually gave up. (Ended up writing a passthrough to our web services that would transform the response in to JSON)
您可以在此处了解有关我的(误)冒险的信息:
You can read about my (mis)-adventures here:
http://www. sencha.com/forum/showthread.php?189537-Sencha-Touch-2.0-nested-XML-parsing
这篇关于读取嵌套的XML以使用hasMany关联,Sencha,ExtJS进行建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!