I make a program that will take the parameters from XML and work with them. Write nothing is necessary, just read. The XML file is on a local disk, not on a server.

How to transfer it to javascript? You can request a file using the input tag, but how to transfer it to the script itself? Or, if this is not possible, tell me, how is it easiest to solve the issue of reading XML from the user's disk directly?

  • Reading the "User Disk" is a prohibited technique. Why do you need it? Use cookies or local storage. You can use new ActiveXObject ("msxml.domdocument"), as a load function, but you will have security problems. You will have to press the buttons to allow, or access to the file may be denied. - nick_n_a
  • 3
    Digging into input[type='file'] and File API - developer.mozilla.org/ru/docs/Using_files_from_web_applications - user207618

1 answer 1

 // Объект для чтения файлов/буферов данных let fr = new FileReader(); // Слушаем изменения document.querySelector('#file').addEventListener('change', function(e){ // Проверочки всякие if(this.files.length === 0) return; let file = this.files[0]; if(file.type !== 'text/xml') // Если тип не XML - выходим return; // Прочиталось! fr.onload = e => { // Используем великое колдунство для парсинга из строки в DOM let dom = (new DOMParser()).parseFromString(fr.result, "application/xml"); console.info('Самый всамделишный DOM из XML:', dom); }; // Читаем как строку объект File (да, тот, что генерирует input[type='file']) fr.readAsText(file); }); 
 <input type='file' id='file' accept='application/xml' />