View Full Version : Trying to display XML document in a table

09-27-2012, 12:22 AM
Hey all,

I'm trying to do what the title says, but it's apparently a lot more difficult than I thought. I'm using the following code:

var xHttp=new xhttprequest();
xHttp.open("GET","www.weebly.com/uploads/2/5/1/0/2510225/custom_themes/334455962819258956/files/onemus.xml", false);
var xDoc=xHttp.responseXML;
var row=document.createElement('tr');

Right now the code is minimal, as I want to get a piece working first and adapt it into a fully fledged script. Note that I do have a <table id='search'> tag on the page itself, and it seems to work fine as long as I'm not dealing with an xml document like this.

Where am I going wrong? Thanks in advance.

Old Pedant
09-27-2012, 01:12 AM
Change to

var xDoc=xHttp.responseXML.documentElement;

09-27-2012, 01:23 AM
Nothing seems to change.

Old Pedant
09-27-2012, 02:32 AM
Well, actually there are several more things wrong.

For starters, this it totally bogus:

var xHttp=new xhttprequest();

It should be

var xHttp = new XMLHttpRequest(); // and watch out for upper/lower case! it DOES matter

And then you have a problem here:


getElementsByTagName returns an *ARRAY*. You can't take the firstChild of an array.

You needed



But none of this is going to work if your own site isn't "www.weebly.com".

JavaScript is NOT allowed to fetch content from foreign sites.

There are several ways around this. The easiest is probably to create a "proxy server" on your own site. Using PHP or ASP or JSP, you would received the AJAX request from the browser and then the server-side code would go to the foreign site, retrieve the content, and return it back to the AJAX code.

09-27-2012, 02:50 AM
Okay, I've fixed the objects in question. I can't believe I missed something like including the array index. However, it still doesn't seem to work.

I'm assuming that my server is weebly.com, as it hosts my actual website (onemus.weebly.com), so I thought it should be okay. If this isn't correct, how would I create a proxy? Something like document.write()?

Thanks for your responses, by the way.

Old Pedant
09-27-2012, 03:06 AM
The same domain policy *DOES* mean the full domain. So, yes, www.weebly.com and onemus.weebly.com are seen as separate domains.

A PHP-based proxy server can be pretty simple.


But it *can* be even simpler than that, if you only want it to always get that same URL.

For example:

$url = "http://www.weebly.com/uploads/2/5/1/0/2510225/custom_themes/334455962819258956/files/onemus.xml";
header ("Content-Type:text/xml");
echo file_get_contents($url);

Old Pedant
09-27-2012, 03:07 AM
By the by, that was another mistake in your original code: You omitted the "http://" from the URL.

09-27-2012, 04:20 AM
I understand php even less than I do javascript. Based on the php documentation, I'm assuming that the function you gave me returns a string with the contents of the xml file. How would I nest the script within my own, and how would I use its output?

EDIT: Some googling told me that I could simply store the results of the php script to a javascript variable. I'm going to go ahead and try that now.

Old Pedant
09-27-2012, 04:35 AM
Well, if you put that into a PHP file named, say, "onemus.php" then (assuming your server supports PHP) you could test it by simply putting the URL "http://onemus.weebly.com/onemus.php" and making sure it give you the same XML that you get when you use the full URL


I need to tell you that I don't use PHP. But that code seems dirt simple enough that I can't see why it wouldn't work.

Old Pedant
09-27-2012, 04:36 AM
Well, sure. You don't have to use AJAX. I just assumed you wanted to, for some reason.

09-27-2012, 04:48 AM
Either way, it seems to be in vain, as I just found out that Weebly doesn't support php. Are there any other workarounds?

EDIT: I was thinking that maybe I could just post the entire xml file on a page not visible to users, and then access it as normal. This would avoid the same domain issue.

09-27-2012, 06:34 AM
I've gone through with my idea. It seems to be okay with actually accessing the html (with xml nested within), but I'm not sure how to 'catch' the response from the server, because before now, I'd been using synchronous. I believe there's some sort of waiting that needs to happen?

My code as of now:

var xHttp=new XMLHttpRequest();
xHttp.open("GET","http://onemus.weebly.com/xml.html", true);
xHttp.responseType = "document";
var row=document.createElement('tr');

Old Pedant
09-27-2012, 09:06 PM
Why did you change to async? Just change that true back to false and be happy.

Yes, you can use async, but then you have to set up a listner, etc., etc. If this is supposed to happen before or as the page is displayed, keep it sync for simplicity.

09-27-2012, 09:46 PM
I was following this document (https://developer.mozilla.org/en-US/docs/HTML_in_XMLHttpRequest), which said that I need to use async to correctly receive an html document.

Old Pedant
09-27-2012, 10:10 PM
Read that more carefully: It only applies to fetching HTML *AND* PARSING the HTML document *AS* HTML.

If your document is still actually XML, than all that doesn't apply.

Why did you rename to document to ".html"? *IS* it now HTML, instead of XML?

09-27-2012, 10:12 PM
It's now xml embedded within a html page. I figured this was the easiest solution, as Weebly doesn't support php, and there's pretty much no other way that I've found that allows me to host an xml document like that.

EDIT: Apparently a mirror of the file at weebly.com is also hosted at onemus.weebly.com...I'm going to try accessing it once again.

EDIT2: Of course it worked. Thanks for your patience and helping me out here.