views:

308

answers:

3

Is there anyway to change the content-type of an XML document, in the XML document?

I'm working with a really old system that passes back HTML (and we are trying to make it return XML). I'm retrieving it from XMLHttpRequest, and I noticed using netcat that it isn't passing back any content-type headers.

When I receive the XMLHttpRequest.responseXML, the responseText exists but the responseXML is null.

I've already checked the XML being returned to see if it is well formed and it appears to be (it's a very short document).

+1  A: 

No. The Content-Type as you refer to it (in the comments to your question) is part of the HTTP headers.

And HTTP is the mere means of transportation for (say) XML documents. They are payload, they know nothing about the HTTP headers, so they can't change them.

What you probably mean is "Is there an equivalent to <meta http-equiv="... in XML. No, there is not. Even HTML can't change the HTTP headers, it can only make the user agent behave differently. This is useful if the HTML file was saved to disk, and upon load no headers are available to the user agent.

In XML, all the necessary information is in the processing instruction (<?xml version="1.0" encoding="UTF-8"?>) at the top of the file. No header info is are needed to load/display it correctly.

Tomalak
A: 

No.

By the time the UA could get to any such tag, it would already have to have decided what kind of document it's parsing.

Gareth
A: 

I figured it out, it's a matter of taking the xhr.responseText string and creating an XML document from it:

function createDOMFromString(sXml){

 var browser = navigator.appName;
 var oXmlDom = null;

 // IE Implementation...
 if(browser == "Microsoft Internet Explorer") {
   oXmlDom=new ActiveXObject("Microsoft.XMLDOM");
   oXmlDom.async="false";
   oXmlDom.loadXML(sXml);
 }
 // FF Implementation...
 else {
  var oParser = new DOMParser();
  oXmlDom = oParser.parseFromString(sXml, "text/xml");
 }
 // TODO: If we need it Safari implementation.

 return oXmlDom;
}

Cheers!

leeand00
Hey I can't check my answer...and it's the one that worked!
leeand00
Nobody can check his own answer (or everybody would). Honestly, shouldn't you have asked the question more clearly in the first place? Your edit made all the difference, the question was close to unintelligible in its original state. Please don't expect your readers to have visionary powers...
Tomalak