Loading XML file from the internet

edited July 2016 in p5.js

I am having problems loading an xml page and parsing it. I am trying to use the loadXML() function but it is not able to access the site.

For example I need to access the content of total_credit here:

https://setiathome.berkeley.edu/show_user.php?userid=110144&format=xml

I tried something like:

var xmlUser, totalcredits;
function preload(){
    xmlUser = loadXML("https:" + "//setiathome.berkeley.edu/show_user.php?userid=110144&format=xml");
}
function setup() {
  createCanvas(300, 100);
  var bobby = xmlUser.getChildren("total_credit");
  totalcredits = bobby[0].getContent();
}

function draw() {
  background(255)
  fill(0);
  text(totalcredits,width*0.3,height*0.1);
}

but it doesn't work. How would you do?

PS: that strange thing on the loadXML() function appears only on the forum, on my program there is written only the link of the site.

Answers

  • Answer ✓

    How are you testing? If you load the page from your local machine you can expect most browsers to block external requests on security grounds - this is a FAQ. See browser console (F12) for any error messages.

    The mangling of URLs on the forum is supposed to be a 'helpful' feature so they link directly to the target. As you've discovered this isn't always desirable :/

  • If I copy-paste the content of the page into an xml file everything works correctly. So my problem is actually to access the page and get the xml by the load function. Right now I am using Firefox, which I thought it had less problems on blocking requests. However you are right, I get the same Access-Control-Allow-Origin error. Is there any more I can do?

  • edited July 2016 Answer ✓

    As I said this is a FAQ... Options are:

    • test on a local server.
    • upload to a server

    Note another possibility is that the target URL deliberately blocks scripted cross-origin requests.

  • thank you very much for the help!

Sign In or Register to comment.