IXMLHttpRequest.responseXml is empty, with no parse error, when responseText contains valid Xml

主宰稳场 提交于 2019-12-03 13:52:58

Ii found the problem

i used Fiddler to save the http response to a text file. After that i could modify the response file, and instruct fiddler to serve my hand-crafted alternatives, rather than going to the original web-site.

After 3 hours of fiddling, i managed to track down the problem in the original http response headers:

HTTP/1.1 200 OK
Cache-Control: max-age=5
Connection: keep-alive
Connection: Transfer-Encoding
Date: Fri, 30 Mar 2012 14:50:50 GMT
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
Expires: Fri, 30 Mar 2012 14:50:55 GMT
Server: Apache/2.2.16 (Unix) PHP/5.3.3 mod_ssl/2.2.16 OpenSSL/1.0.0d mod_perl/2.0.4 Perl/v5.12.0
X-Powered-By: PHP/5.3.3

should be:

HTTP/1.1 200 OK
Cache-Control: max-age=5
Connection: keep-alive
Connection: Transfer-Encoding
Date: Fri, 30 Mar 2012 14:50:50 GMT
Transfer-Encoding: chunked
Content-Type: text/xml; charset=UTF-8
Expires: Fri, 30 Mar 2012 14:50:55 GMT
Server: Apache/2.2.16 (Unix) PHP/5.3.3 mod_ssl/2.2.16 OpenSSL/1.0.0d mod_perl/2.0.4 Perl/v5.12.0
X-Powered-By: PHP/5.3.3

Once i found the problem, i was able to back-find the documentation that explain the behavior:

The supported MIME types for MSXML 6.0 are:

  • "text/xml"
  • "application/xml"
  • or anything that ends with "+xml", for example "application/rss+xml"

The RSS feed i'm fetching is actually a Resource Definition Format (RDF) feed, where the content type is supposed to be:

application/rdf+xml

Their use of:

text/html

is wrong on so many levels.

So the behavior i'm experiencing is by design; although frustrating - as there's no easy way to know if the responseXml is "valid".

  • the responseXml object will be assigned
  • the parseError object will be assigned
  • the parseError.ErrorCode is zero
  • the responseXml.documentElement will be nil

I had the same problem with YouTube services.

The responseXml object is dependent on the content-type/MIME of the response.
You could examine the response Content-Type e.g: if http.getResponseHeader('Content-Type') contains text/xml or application/xml only then you can refer to http.responseXml, otherwise it will be empty (see MSDN Remarks). Also note that the responseXml parser validation features are always turned off, for security reasons.

But, the http.responseText will always have the xml text, no matter what content type is in the response, so you can always use a new instance of DOMDocument to load the xml e.g:

...
http := CoXMLHTTP60.Create; // or CoServerXmlHttpRequest.Create 
http.open('GET', szUrl, False, '', '');
http.send(EmptyParam);
Assert(http.Status = 200);

doc := CreateOleObject('Msxml2.DOMDocument.6.0') as DOMDocument60; 
doc.async := False;
doc.loadXML(http.responseText); // <- load XmlHttpRequest.responseText into DOMDocument60 and use it
Assert(doc.parseError.errorCode = 0);

// do useful things with doc object...

Well, this works in Delphi XE and Delphi 7:

procedure TForm1.Button1Click(Sender: TObject);
var
    szUrl: string;
    http: IXMLHTTPRequest;
    doc: {$ifndef UNICODE}WideString{$else}string{$endif};
begin
    szUrl := 'http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_all.xml';

    http := CoXMLHTTP60.Create; //or CoServerXmlHttpRequest.Create
    http.open('GET', szUrl, False, '', '');
    http.setRequestHeader('Content-Type', 'text/xml;charset=UTF-8');
    http.send(EmptyParam);

    Assert(http.Status = 200);

    doc := UTF8Encode(http.responseText);

    Memo1.Lines.text := doc;
//  ShowMessage('"'+doc.xml+'"');
end;

Hope it works for you in Delphi 5, too. Of course, any unicode characters are going to turn into ? on you, in non-unicode delphi versions.

You are retreiving the xml from the DOMDocument object itself, but you should be grabbing it from the first node in the document's tree instead, eg:

doc := http.responseXml as DOMDocument60; 
Assert(doc.parseError.errorCode = 0); 
ShowMessage('"' + doc.DocumentElement.childNodes.Item(0).xml + '"'); 

Microsoft's own examples in the documentation for DOMDocument and the xml property show exactly that kind of logic.

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