0

This is going to have a lot of context as I'm not 100% sure how to ask what I'm asking.

I've been mocking up a website (I say mocking up because nothing is online yet). I have a folder with a series of images and an XML document (though it could be anything) detailing the order that the images need to be displayed. Is an XML document the best way to do this? Is there a better way?

I've been having trouble reading the XML document on chrome which is apparently because of local file security risks. Why is this a security risk when I can use other local sources (images) fine? This works on other browsers, but I can't get non local files working on any browser.

There are many examples such as: http://www.w3schools.com/xml/tryit.asp?filename=tryxml_parsertest

The problem is that these are assuming the files are local:

xmlhttp.open("GET", "note.xml", false);

and when they provide them they never provide them as a page:

www.notarealwebsite.com/note.xml

Just as something you can copy. So I can't check if the javascript works properly.

I feel like I'm being stupid. Main question: How do I test my xml parser?

Edward Kotarski
  • 594
  • 5
  • 11

2 Answers2

0
xmlhttp.open("GET", "note.xml", false);

is not a local file . It means the file is hosted on w3schools .
The file path is relative to the page i.e page and xml are stored in the same location on the web server.

File path is http://www.w3schools.com/xml/note.xml

You need to host your file and xml similarly on a webserver and then access the page from your web server.

You can setup your own local web server for development too.(most are free)
https://www.maketecheasier.com/setup-local-web-server-all-platforms/ http://www.hongkiat.com/blog/create-webserver-on-pc-the-fast-way/

If you don't have a web server for now :
you can put your xml as string variable in javascript and load it.
(no server needed.)

//replace your xml here
    var xmlStr = '<books>' +
              '  <book title="A Tale of Two Cities"/>' +
              '  <book title="1984"/>' +
              '</books>';

    var xmlDoc = parseXml(xmlStr);
    // rest of code follows

You can use parseXml function from this answer XML parsing of a variable string in JavaScript

Community
  • 1
  • 1
Amitd
  • 4,769
  • 8
  • 56
  • 82
0

Is an XML document the best way to do this? Is there a better way?

That's really subjective.

Why is this a security risk when I can use other local sources (images) fine?

It is much, much harder to cause security problems by displaying an image from the user's hard drive in a web page then it is to read data from the user's hard drive with JavaScript.

What if the HTML document was an email attachment? And the file that was read was the standard file path for some popular accounting software's data files? If someone opens the email attachment, JavaScript could send their financial records to the evil attacker.

If you want to test XHR stuff, then run a local HTTP server.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335