Read a local xml file to a string in Javascript


I'm surprised to see that this is hard to do, but i haven't found a single way to do that. Basically i have a directory that contains:

--index.html --script.js --file.xml

And i want to read the content of the <strong>file.xml</strong> to a JS string for parsing. The only method i found of doing so was by using a synchronous xmlhttp object which is disabled by default in my browser. Is there another (preferably easy) way of reading a file to a string in js?


So, I might be a little late to the party, but this is to help anybody else who's been ripping his/her hair out looking for a solution to this.

First of all, CORS needs to be allowed in the browser if you're not running your HTML file off a server. Second, I found that the code snippets most people refer to in these kind of threads don't work for loading local XML-files. Try this (example taken from the official docs):

var xhr = new XMLHttpRequest(); xhr.open('GET', 'file.xml', true); xhr.timeout = 2000; // time in milliseconds xhr.onload = function () { // Request finished. Do processing here. var xmlDoc = this.responseXML; // <- Here's your XML file }; xhr.ontimeout = function (e) { // XMLHttpRequest timed out. Do something here. }; xhr.send(null);

The method (1st arg) is ignored in xhr.open if you're referring to a local file, and async (3rd arg) is true by default, so you really just need to point to your file and then parse the result! =)

Good luck!


  • Java: fastest way to do random reads on huge disk file(s)
  • Solr copy data from “crawler” core into “search” core
  • memcpy of a part of a struct
  • Using Reactive Extension for certain KeyPress sequences?
  • python, confused in decorate and closure
  • ScraperWiki: How to create and add records with autoincrement key
  • DropShadowPanel adapt to button template style
  • Scroll down whole browser window
  • time_t conversion format question
  • IE11 textarea loses focus if another textarea is disabled
  • How can I reset dropdown data if modal closed on vue component?
  • Synchronize windows folders
  • Does Apple allow the usage of sysctl.h within iOS applications?
  • what makes a request a new request in asp.net C#
  • Possible to “watch” both HAML and SASS at the same time?
  • How to getText() from the input field of an angularjs Application
  • Android changing fragment order inside FragmentPagerAdapter
  • Jquery popup on mouse over of calendar control
  • System.InvalidCastException: Specified cast is not valid
  • Installed module is empty
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How do I get HTML corresponding to current DOM tree?
  • How to create a file in java without a extension
  • Read a local file using javascript
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Paperclip, set path outside of rails root folder
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Modifying destination and filename of gulp-svg-sprite
  • GridView Sorting works once only
  • AT Commands to Send SMS not working in Windows 8.1
  • PHP: When would you need the self:: keyword?
  • How to disable jQuery.jplayer autoplay?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • C# - Getting references of reference
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?
  • How do I use LINQ to get all the Items that have a particular SubItem?