Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

508
Views
Read and edit local xml-file in Javascript

I have a directory that contains

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

and I want to read the content of the file.xml as a string in order to replace a few words within the xml-file. (Later on I would like to send the edited xml-file as e-mail).

I managed to access to xml-file, but did not manage to save the whole content in a string and/or replace some known keywords within the xml.

This is where I got so far:

var oXHR = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');

function reportStatus2() {
  if (oXHR.readyState == 4)               // REQUEST COMPLETED.
     console.log(this.responseXML)        // This gets me the XML-document, but I want the complete xml content as string
}
oXHR.onreadystatechange = reportStatus2;    
oXHR.open("GET", "../file.xml", true);
oXHR.send();

I found so many posts on that topic here, but none was able to answer my question! Any ideas?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Look at the documentation for XMLHttpRequest.

Here is the documentation for the property you are reading:

XMLHttpRequest.responseXML Read only

Returns a Document containing the response to the request, or null if the request was unsuccessful, has not yet been sent, or cannot be parsed as XML or HTML. Not available in workers.

Now look around. Just three entries earlier it says:

XMLHttpRequest.responseText Read only

Returns a DOMString that contains the response to the request as text, or null if the request was unsuccessful or has not yet been sent.

So use that instead.


That said, XML is a structured data format with some fairly complex rules and it you should use DOM to edit it and not string manipulation (which is error prone and much more likely to result in invalid XML in your output).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!