Как разобрать XML-ответ с помощью Sencha Touch?

Ext.Ajax.request({
  url: 'http://bar.com/api/foos/',
  success: function(response, opts) {
    // How parse the response containing the XML?
  }
});

Я пробовал возиться с XmlReader, но не зашел слишком далеко.


person randomguy    schedule 02.07.2011    source источник


Ответы (2)


Я действительно новичок в Sencha Touch, но у меня есть XML для чтения и отображения в порядке, проблема заключалась в том, что вы не можете получить доступ к междоменному xml (хотя я слышал, что если вы компилируете в PhoneGap есть способ ), поэтому я поместил все свои файлы в одно и то же место и проанализировал XML с помощью PHP-скрипта на своем сервере (в свою очередь, оставив его локальным). См. пример ниже:

<!DOCTYPE html>
<html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>XML Example</title>
        <script src="../sencha-touch-debug.js" type="text/javascript">
        </script>
        <link href="../resources/css/sencha-touch.css" rel="stylesheet" type="text/css">
        <script type="text/javascript">
            new Ext.Application({
                name: 'xmlexample',
                launch: function(){
                    Ext.regModel('Profile', {
                        fields: ['nobea','nobeb','nodec','noded'] //etc...
                    });

                    this.stores.profiles = new Ext.data.Store({
                        model: 'Profile',
                        autoLoad:true,
                        implicitIncludes: true,
                        proxy: {
                            type: 'ajax',
                            url : 'http://www.yourwebsite.co/php/xmlparse.php?url=http://externalxmllink',
                            reader: {
                                type  : 'xml',
                                root  : 'profile',
                                record: 'profile'
                            }
                        }
                    });
                var productTpl = new Ext.XTemplate(
                    '<tpl for=".">',
                        '<div class="data">{nobea}</div>',
                        '<div class="data">{nobeb}</div>',
                '<div class="data">{nobec}</div>',
                        '<div class="data">{nobed}</div>',
                    '</tpl>'
                );    
                new Ext.Panel({
                    fullscreen: true,
                    items: new Ext.DataView({
                        store: this.stores.profiles,
                        tpl: productTpl,
                        itemSelector: 'product-selected'
                        //other config goes here
                    })
                });
            }
        });        
        </script>
    </head>
    <body>
    </body>
</html>

Таким образом, пример XML-файла будет выглядеть так:

<?xml version="1.0" encoding="utf-8"?>
<profile>
<nodea>text</nodea>
<nodeb>text</nodeb>
<nodec>text</nodec>
<noded>text</noded>
</profile>

А вот xmlparse.php

<?php
// Set your return content type
header('Content-type: application/xml');

// Website url to open
//$daurl = 'http://YOURXMLLINK';
$daurl = $_GET['url'];

// Get that website's content
$handle = fopen($daurl, "r");

// If there is something, read and return
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
    }
    fclose($handle);
}
?>

Надеюсь это поможет :)

person Sam Tassell    schedule 08.07.2011

У вас была пара опечаток (nobed вместо noded)

Это работает:

<!DOCTYPE html>
<html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>XML Example</title>
        <script src="../touch/sencha-touch.js" type="text/javascript"></script>
        <link rel="stylesheet" href="../touch/resources/css/sencha-touch.css" />
        <script type="text/javascript">
            new Ext.Application({
                name: 'xmlexample',
                launch: function(){
                    Ext.regModel('Profile', {
                        fields: ['nodea','nodeb','nodec','noded'] //etc...
                    });

                    this.stores.profiles = new Ext.data.Store({
                        model: 'Profile',
                        autoLoad:true,
                        implicitIncludes: true,
                        proxy: {
                            type: 'ajax',
                            url : 'data.xml',
                            reader: {
                                type  : 'xml',
                                root  : 'profile',
                                record: 'profile'
                            }
                        }
                    });
                var productTpl = new Ext.XTemplate(
                    '<tpl for=".">',
                        '<div class="data">{nodea}</div>',
                        '<div class="data">{nodeb}</div>',
                '<div class="data">{nodec}</div>',
                        '<div class="data">{noded}</div>',
                    '</tpl>'
                );    
                new Ext.Panel({
                    fullscreen: true,
                    items: new Ext.DataView({
                        store: this.stores.profiles,
                        tpl: productTpl,
                        itemSelector: 'product-selected'
                        //other config goes here
                    })
                });
            }
        });        
        </script>
    </head>
    <body>
    </body>
</html>
person Bas Jaburg    schedule 13.07.2011