1. #1
    Sencha User
    Join Date
    May 2013
    Posts
    19
    Vote Rating
    0
    Hasret is on a distinguished road

      0  

    Default Answered: Parsing an xml within a json file

    Answered: Parsing an xml within a json file


    Hi guys !
    I have just recently started working with Sencha Touch 2. I am having problem in
    reading a JSON-file. One of the JSON-objects happens to be some stringified XML i neeed to change the proxy to read json, and handle the result correctly , to parse the result to xml , how can i deal with that ?

    my json file :

    Code:
    {"status":"ok","config":"<?xml version=\"1.0\"?>\n <application id=\"4\" name=\"name\" ><modules> <module id=\"32\" > <\/module> ..... <\/modules> <\/application>\n"}

  2. You would need to create a function to go through the xml and pull out the fields you need.
    Something like this:
    Code:
    getString: function(path, root) {
        var output = '';
        var values = Ext.DomQuery.select(path, root);
        if(values[0]){
            if (values[0].firstChild) {
                output = values[0].firstChild.nodeValue;
            }
        }
        return output;
    },
    getStringArray: function(path, root) {
        var values = Ext.DomQuery.select(path, root), 
        valuesCount = values.length, 
        valuesArray = new Array(), 
        i;
        
        for (i = 0; i < valuesCount; i++) {
            if (values[i].firstChild) {
                valuesArray.push(values[i].firstChild.nodeValue);
            }
        }
        
        return valuesArray;
    }

  3. #2
    Sencha User jerome76's Avatar
    Join Date
    Apr 2012
    Location
    New Jersey
    Posts
    1,149
    Answers
    132
    Vote Rating
    99
    jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice

      0  

    Default


    I am having trouble understanding your problem. You have a JSON object with a value that is an xml string. Do you just want to parse that xml string value?

  4. #3
    Sencha User
    Join Date
    May 2013
    Posts
    19
    Vote Rating
    0
    Hasret is on a distinguished road

      0  

    Default


    yes that's it , and i want to create a model to use elements of that xml file, i've made that :

    Code:
    Ext.define("MyApp2.store.TabBarStore", {
        extend: "Ext.data.Store",
        requires: ["Ext.data.proxy.JsonP", "Ext.data.reader.Xml"],
        config: {
            model: "MyApp2.model.TapBarModel",
            autoLoad: true,
            id: 'TabBarModel',
            proxy: {
                type: 'jsonp',
                url: 'http://mysite.com/api/applications/894/config?format=jsonp&lang=fr&access_token=' + token,
                reader: {
                    type: 'json',
                    rootProperty: 'config'
    
    
                }
            }
            ,
            listeners: {
                load: function(me, records, successful, operation, eOpts) {
    
    
    Ext.data.JsonP.request({
        url:'http://mysite.com/api/applications/894/config',
        params: {
           format: 'jsonp',
          lang: 'fr',
          access_token: token
    
    
        },
        success: function(result) {
           
            var xml = result.config;
     console.log(xml);
            //How to parse the xml ? & use it with a model ???? 
    
    
        }
    });
    
    
                    }
                }
            }
    
    
    });

  5. #4
    Sencha User jerome76's Avatar
    Join Date
    Apr 2012
    Location
    New Jersey
    Posts
    1,149
    Answers
    132
    Vote Rating
    99
    jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice

      0  

    Default


    What do you get when you log the var xml? Shouldn't you just be able to grab the xml string in the load event by:
    Code:
    var xml = [];
    Ext.each(records, function(rec){
        xml.push(reg.get('config'));
    });
    Or something like that.

  6. #5
    Sencha User
    Join Date
    May 2013
    Posts
    19
    Vote Rating
    0
    Hasret is on a distinguished road

      0  

    Default


    I'm getting xml , i dont need a loop its one record , but how to use it , like a store with a model ??

  7. #6
    Sencha User jerome76's Avatar
    Join Date
    Apr 2012
    Location
    New Jersey
    Posts
    1,149
    Answers
    132
    Vote Rating
    99
    jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice

      1  

    Default


    You would need to create a function to go through the xml and pull out the fields you need.
    Something like this:
    Code:
    getString: function(path, root) {
        var output = '';
        var values = Ext.DomQuery.select(path, root);
        if(values[0]){
            if (values[0].firstChild) {
                output = values[0].firstChild.nodeValue;
            }
        }
        return output;
    },
    getStringArray: function(path, root) {
        var values = Ext.DomQuery.select(path, root), 
        valuesCount = values.length, 
        valuesArray = new Array(), 
        i;
        
        for (i = 0; i < valuesCount; i++) {
            if (values[i].firstChild) {
                valuesArray.push(values[i].firstChild.nodeValue);
            }
        }
        
        return valuesArray;
    }

  8. #7
    Sencha User
    Join Date
    May 2013
    Posts
    19
    Vote Rating
    0
    Hasret is on a distinguished road

      0  

    Default


    Quote Originally Posted by jerome76 View Post
    You would need to create a function to go through the xml and pull out the fields you need.
    Something like this:
    Code:
    getString: function(path, root) {
        var output = '';
        var values = Ext.DomQuery.select(path, root);
        if(values[0]){
            if (values[0].firstChild) {
                output = values[0].firstChild.nodeValue;
            }
        }
        return output;
    },
    getStringArray: function(path, root) {
        var values = Ext.DomQuery.select(path, root), 
        valuesCount = values.length, 
        valuesArray = new Array(), 
        i;
        
        for (i = 0; i < valuesCount; i++) {
            if (values[i].firstChild) {
                valuesArray.push(values[i].firstChild.nodeValue);
            }
        }
        
        return valuesArray;
    }
    Thank u for ur help , its a little complicated for me , i have an other idea , if i save the string XML to an xml file , and then parse it using an xml reader , how can i save my string to a file string.xml ???


  9. #8
    Sencha User jerome76's Avatar
    Join Date
    Apr 2012
    Location
    New Jersey
    Posts
    1,149
    Answers
    132
    Vote Rating
    99
    jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice jerome76 is just really nice

      1  

    Default


    I think either way would be complicated. You can only use the xml reader if you are using a proxy to retrieve that xml file from a server, or in memory. The xml writer doesn't have much documentation, but you would need a Model set up correctly to format the xml, which I am unsure of how to do in this case. This question has been out for a few months, but has no answer yet. My best guess is to follow that topic for any responses and in the meantime try to play with the code nova969 posted.

    I think that the getStringArray function would be better to use since you won't have to make another couple calls to the server. An example of using the getStringArray would be like this in your case (this is just a skeleton code):
    Code:
    var json = ..., /*say you have your json object here*/
    	xml = json.config;
    	
    /* now the var xml will have the xml string:
     * "<?xml version=\"1.0\"?>\n <application id=\"4\" name=\"name\" ><modules> <module id=\"32\" > <\/module> ..... <\/modules> <\/application>\n"
     */
    var modules = this.getStringArray('module', xml);
    //this will fill the modules array with all the values from the module tags
    You can play around with the getStringArray until it builds your values right.

  10. #9
    Sencha User
    Join Date
    May 2013
    Posts
    19
    Vote Rating
    0
    Hasret is on a distinguished road

      0  

    Default


    thank you very much for all thse informations , i 'll play with that til i found the right conbinaison ...

    I've tried the code below , it gives me an error :

    Code:
    Uncaught TypeError: Cannot call method 'querySelectorAll' of null 
    
    
    i put it in my controller ,just like this :
    Code:
    Ext.define('MyApp2.controller.ApplicationViewController', {
        extend: 'Ext.app.Controller',
        requires: ["MyApp2.store.ApplicationStore"],
        init: function() {
    
    
            Ext.data.JsonP.request({
                scope: this,
                url: 'myurl',
                params: {
                    format: 'jsonp',
                },
                success: function(result) {
    
    
                    var xml = result.config;
                 //   var oParser = new DOMParser();
                 //   var data = oParser.parseFromString(xml, "text/xml");
                  //  var modules = this.getStringArray('modules', xml);
                  console.log(xml);
                    console.log(Ext.DomQuery.select('modules', xml));
                    // Ext.getStore("ApplicationStore").add(data);
                }
    
    
            });
    
    
        },
        getString: function(path, root) {
            var output = '';
            var values = Ext.DomQuery.select(path, root);
            if (values[0]) {
                if (values[0].firstChild) {
                    output = values[0].firstChild.nodeValue;
                }
            }
            return output;
        },
        getStringArray: function(path, root) {
            var values = Ext.DomQuery.select(path, root),
                    valuesCount = values.length,
                    valuesArray = new Array(),
                    i;
    
    
            for (i = 0; i < valuesCount; i++) {
                if (values[i].firstChild) {
                    valuesArray.push(values[i].firstChild.nodeValue);
                }
            }
    
    
            return valuesArray;
        },
    No problem with getting data from the request ...but no values returned from the function ..

Thread Participants: 1

Tags for this Thread