leeand00
leeand00

Reputation: 26392

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

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).

Upvotes: 0

Views: 2225

Answers (3)

leeand00
leeand00

Reputation: 26392

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!

Upvotes: 0

Gareth
Gareth

Reputation: 138072

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.

Upvotes: 0

Tomalak
Tomalak

Reputation: 338228

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.

Upvotes: 2

Related Questions