Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

loading XML with .html extension on website

Josh CodeJosh Code Posts: 675Member
I'm trying to make a website that uses the same method of requesting an XML document as with AJAX. I want to use the extension .html for the XML document, though.

The problem I keep finding is that if I leave an xml document with the extension .html, the requested document is left unparsed. The root document node is left without any child nodes. The requested document is XML compliant and loads fine using the .xml extension, however.

My only guess is that the web browser somehow thinks that extension shouldn't be for an xml document so it doesn't even try to parse it as xml. The documents start with <?xml version="1.0"?> but that doesn't solve anything.

The web browsers I've tried are FireFox and Internet Explorer. Both don't log any JavaScript error messages but simply don't parse.



Let's explain the code below.
The following script is for the loading and parsing process of the document.
1. importXML(url) is the first called to start the loading process.

2. If the browser lets the onload method be specified, when the document is fully loaded, loadHTMLComponentsFor is called directly.

3. If the browser uses the window.ActiveXObject, the more flexible delayedLoadHTMLComponentsFor function is called which keeps checking on a certain interval if the document was loaded. When loaded, calls loadHTMLComponentsFor.

Here is the related functions:
[code]

/**
Called when a specific Document has been loaded.
This replaces all "component" elements with matching src attributes
with the expanded form by substituting the transformed loaded object

@param url is the url from which the document was loaded
@param doc is the loaded DOM Document object
*/
function loadHTMLComponentsFor(url,doc)
{
if (!checkContainsBody(doc))
{

alert("The component document at: "+url
+" seems to be missing a body element. "
+"If it has a body element, also ensure that it uses .xml "
+"as its extension and is valid xml.");
return;
}

var elements = document.getElementsByTagName("component");

for (var i=0;i<elements.length;i++)
{
var n = elements[i];
var source = n.getAttribute('src');

if (source==url)
{
var p = n.parentNode;
if (!p)
{
alert("parent node not set for a component DOM Element.");
return;
}
params[i]=getParametersFromDocument(doc);
params[i]=mergeParametersWithComponentElementProperties(params[i],n);

var newElement = createElementFrom(n);
// process newElement for templates
initializeElementExtended(i,newElement);

p.replaceChild(newElement,n);
}
}

}

/**
Keeps trying to load from the document. When a 'body' element is found,
it is assumed loaded and the task is passed to loadHTMLComponentsFor.
If the time runs out, it just gives up.

@param url is the address for the document that is being loaded
@param doc is the XML Document object that may have been loaded
@param maxTime is the maximum number of miliseconds before the delayed call should time out
*/
function delayedLoadHTMLComponentsFor(url,maxTime)
{
if (maxTime < 0)
{
alert("Loading of document timed out. Component source URL: "+url);
return;
}

if (checkContainsBody(componentDocuments[url]))
{
loadHTMLComponentsFor(url,componentDocuments[url]);
return; // finished successfully
}

var intervalSize = 100; // 1/10 of a second delay between each check

maxTime-=intervalSize;
setTimeout('delayedLoadHTMLComponentsFor("'+url+'",'+maxTime+')',intervalSize);
}



/**
Tries to create a new XML Document object.
This should work fine in Firefox, Netscape, and Internet Explorer.
If AJAX works, this should work.
*/
function createNewXMLDocument()
{
if (document.implementation && document.implementation.createDocument)
{
return document.implementation.createDocument("", "", null);
}
else if (window.ActiveXObject)
{
var result = new ActiveXObject("Microsoft.XMLDOM");
return result;
}
else
{
alert('Your browser can't handle this script');
return;
}
}

/**
Returns a document after loading from the specified url
Returns null if not successful
*/
function importXML(url)
{
if (componentDocuments[url])
{ // already loading this document
return;
}
componentDocuments[url] = createNewXMLDocument();
if (!componentDocuments[url])
{
alert("There was a problem creating a blank XML Document object.");
return;
}

if (window.ActiveXObject)
{
// this should work in any web browser
delayedLoadHTMLComponentsFor(url,20000);
// maximum of 20 seconds to load the documents
}
else
{

componentDocuments[url].onload = function()
{
/* the following will be invoked when the new
document finishes loading
*/
loadHTMLComponentsFor(url,componentDocuments[url]);
};
}
try
{
componentDocuments[url].load(url);
}
catch (e)
{
alert("problem invoking load method of an XML Document. "
+"Check that there is a component document at '"+url+"'");
}
}
[/code]


Comments

  • netgertnetgert Posts: 331Member
    Most probably the problem is not with the client, but the server. The extension "html" results in "text/html" MIME type, which is not parsed into a XML document by the browsers. "xml" extension however results in "text/xml", which will be parsed.
    Configure your server to send the correct Content-Type header for ".html" files if you want them parsed.

    G
Sign In or Register to comment.