This is a migrated thread and some comments may be shown as answers.

How do handle invalid XML ?

2 Answers 229 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 19 May 2012, 10:39 AM
When something goes wrong with the read: on server it will send an HTML message and not deliver XML.  successCheck is never run and developer tools shows me a "Breaking on JScript runtime error -- invalid XML: ...the server response..."

Where should I put a try/catch ? Or How should I otherwise make sure the server response is actually XML before handing it off to the xml parser?
var crud = "/MyCollections";
var successCheck = function (data) { alert(data);   }
 
var dataSource1 = new kendo.data.DataSource ({
transport: {
read:
{ cache: false, url: crud, data: { action: 'list' }, success: successCheck },
update:
{ cache: false, url: crud, data: { action: 'update' }, success: successCheck },
destroy:
{ cache: false, url: crud, data: { action: 'delete' }, success: successCheck },
create:
{ cache: false, url: crud, data: { action: 'create' }, success: successCheck }
},
schema: {
type: 'xml',
data: '/TABLE/TREEVIEWS',
model: {
fields: {
treeview: "treeview/text()",
name: "name/text()",
timestamp: "timestamp/text()"
} } } });



Thanks,
Richard

2 Answers, 1 is accepted

Sort by
0
rlapao
Top achievements
Rank 1
answered on 22 May 2012, 12:01 PM
HI Richard,

Did you manage to solve your problem?
I'm have a similar one.

Thanks,
Ricardo
0
Richard
Top achievements
Rank 1
answered on 22 May 2012, 02:33 PM

Hi Ricardo:

I modified the data.js source in three places.

Two were as follows:

        data = that.reader.parse(data);<BR>

changed to

try /* RAD */
{
    data = that.reader.parse(data);
}
catch (e) {
    if (options.parseCatch) {
        options.parseCatch(e, data);
        return;
    }
    throw e;
}

  

And one was

  response = that.reader.parse(response);<BR>

  changed to

try /* RAD */
{
    response = that.reader.parse(response);
}
catch (e) {
    if (options.parseCatch) {
        options.parseCatch(e, response);
        return;
    }
    throw e;
}

and in my dataSource configuration I specify the parseCatch function

schema: {
    type: 'xml',
    data: '/TABLE/TREEVIEWS',
    model: {
        id: "treeview",
        fields: {
            treeview:   { editable:false, field: "treeview/text()" },
            name:       { editable:true,  field: "name/text()", defaultValue:"" },
            timestamp:  { editable:false, field: "timestamp/text()", defaultValue:"" }
        }
    }
},
parseCatch: function (e,data) {
    // display response that phailed xml parse
    var w = window.open();
    w.document.write(data);
    w.document.close();
},



Now I can easily see what weirdness the sever might be doing.
A more advanced version might
1. return false so the data.js mod returns after parseCatch
2. return some XML document to be a parse replacement

Tags
Data Source
Asked by
Richard
Top achievements
Rank 1
Answers by
rlapao
Top achievements
Rank 1
Richard
Top achievements
Rank 1
Share this question
or