Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Trying to display XML document in a table

    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:

    Code:
    var xHttp=new xhttprequest();
    	xHttp.open("GET","www.weebly.com/uploads/2/5/1/0/2510225/custom_themes/334455962819258956/files/onemus.xml", false);
    	xHttp.send();
    	var xDoc=xHttp.responseXML;
    	var row=document.createElement('tr');
    	row.appendChild(document.createTextNode(xDoc.getElementsByTagName('type').firstChild.nodeValue));
    	document.getElementById('search').appendChild(row);
    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Change to
    Code:
    var xDoc=xHttp.responseXML.documentElement;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Nothing seems to change.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Well, actually there are several more things wrong.

    For starters, this it totally bogus:
    Code:
    var xHttp=new xhttprequest();
    It should be
    Code:
    var xHttp = new XMLHttpRequest(); // and watch out for upper/lower case!  it DOES matter
    And then you have a problem here:
    Code:
    xDoc.getElementsByTagName('type').firstChild
    getElementsByTagName returns an *ARRAY*. You can't take the firstChild of an array.

    You needed
    Code:
    xDoc.getElementsByTagName('type')[0].firstChild
    BUT...

    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    green_meep (09-27-2012)

  • #5
    New Coder
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

    http://www.webresourcesdepot.com/cro...ple-php-proxy/

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

    For example:
    Code:
    <?php
    $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);
    ?>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    By the by, that was another mistake in your original code: You omitted the "http://" from the URL.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New Coder
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.
    Last edited by green_meep; 09-27-2012 at 03:30 AM.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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
    "http://www.weebly.com/uploads/2/5/1/0/2510225/custom_themes/334455962819258956/files/onemus.xml"

    *********

    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Well, sure. You don't have to use AJAX. I just assumed you wanted to, for some reason.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    New Coder
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.
    Last edited by green_meep; 09-27-2012 at 04:06 AM.

  • #12
    New Coder
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:
    Code:
    	var xHttp=new XMLHttpRequest();
    	xHttp.open("GET","http://onemus.weebly.com/xml.html", true);
    	xHttp.responseType = "document";
    	xHttp.send();
    	xDoc=xHttp.responseXML;
    	var row=document.createElement('tr');
    	row.appendChild(document.createTextNode(xDoc));
    	document.getElementById('search').appendChild(row);

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    New Coder
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I was following this document, which said that I need to use async to correctly receive an html document.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •