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

How kendo.data.xml.js parses HTML varies by browser

1 Answer 139 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 10 Jun 2012, 12:00 PM
I have a server delivering a data query in XML format. When the query fails or is ill-formed the server returns a 200 OK response that is a error message in the guise of an HTML page.  

In IE the parse of the response fails, and in Chrome it does not.

The data.xml.js parse: method uses jQuery (I am using 1.7.1) $.parseXML which in turn uses
  • window.DOMParser, or 
  • ActiveXObject( "Microsoft.XMLDOM" )
The HTML is parsed fine by window.DOMParser because the HTML is valid and valid HTML parses as XML.
The Microsoft.XMLDOM parser does not like the HTML and its .LoadXML() returns a null.

So the question is should data.xml.js handle the HTML ? I would suggest no, or at least make it controllable through an option since some future task may require using data found in some xpath of an HTML response.

To fail the HTML response I made this adjustment in kendo.data.xml.js parse: 
    documentElement = xml.documentElement || $.parseXML(xml).documentElement;

changed to 
  documentElement
    =   xml.documentElement
    || ( xml.substr(0,5)==
'<?xml' 
       ? $.parseXML(xml).documentElement
       : jQuery.error(
"Invalid XML: " + xml )
       )
    ;

A schema: property could be used to control the strictness of parsing.

schema: { type: 'xml', strict: true, ... }  // strict (default) requires <?xml lead-in, false allows for any lead-in
schema: { type: 'xml', leadin: '<?DOCTYPE HTML', ... }  // string (or array of string) for specifying allowed lead-ins

Happy coding,
Richard

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 11 Jun 2012, 07:19 AM
Hi Richard,

The way I see it you can handle this case by not returning an error response with 200 HTTP code in the first place. If you don't the error event will be raised.

There are indeed differences between the XML parsing behavior in older versions of IE (where DomParser is not supported) and standards compliant browsers. However I don't think that a simple check for <?xml would help - there are quite a lot of valid XML documents which don't start with this.

Regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Data Source
Asked by
Richard
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or