Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

xml parsing - Read nested XML to model with hasMany association, Sencha, ExtJS

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>

The models used are as follows:

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'


            }
        });

Store used as below:

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...