How to get Xml data by using ajax

巧了我就是萌 提交于 2020-01-17 06:10:42

问题


I am trying to get my xml data by using ajax call. But I am getting only ROOT Node value data in my result.responseXML. Not able to access ITEM tag data.

My XML.

<ROOT UN="ABC" Total="28">
    <ITEM Val = "1" data = "name1" />
    <ITEM Val = "2" data = "name2" />
    <ITEM Val = "3" data = "name3" />
    <ITEM Val = "4" data = "name4" />
    <ITEM Val = "5" data = "name5" />
</ROOT>

Here is what I am trying.

Ext.Ajax.request({
        url : url,
        method: 'GET',
        success: function ( result, request )
            { 
               debugger;
                result.responseXML
            }
    });

In responseXML I am getting <ROOT UN="ABC" Total="28" ></ROOT>

Can Anybody help me what I am doing wrong and how to coorrect that. How to get Item tag also.


回答1:


In Ext js, the best way to read xml data is to use a store.

  1. Create a store and define the fields (the mapping property (@Val) is used to indicate this is an attribute instead of a child element);
  2. Use an ajax proxy to load the data;
  3. Use an xml reader to read the data. In Ext js 6, you define the name of the root element and name of the elements you want to obtain;
  4. Load the store and show the value of the first element in the console.

var itemStore = Ext.create('Ext.data.Store', {
    fields: [{name: 'val', mapping: '@Val'}, 
        {name: 'data', mapping: '@data'}],
    proxy: {
        type: 'ajax',
        url: 'data1.xml',
        method: 'GET',
        reader: {
            type: 'xml',
            record: 'ITEM',
            rootProperty: 'ROOT'
        }
    }
});

itemStore.load(function(records, operation, success) {
    var item1 = itemStore.first();
    console.log("First item " + item1.get('val'));
});

Check the fiddle here: https://fiddle.sencha.com/#view/editor&fiddle/1vd6



来源:https://stackoverflow.com/questions/43894318/how-to-get-xml-data-by-using-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!