0

The browser is not reading the xml file because of ampersands like "&". I'd like to get the xml file and replace all "&" to "and". Is it possible?

I tried using replace function but it's not working: var xml = xml.replace(/&/g, "and");

var request = new XMLHttpRequest();
request.open("GET", "/ODDS/odds3.xml", false);
request.send();
var xml = request.responseXML;
var xml = xml.replace(/&/g, "and");

var txt = "";
var txt2 = "";
var txt3 = "";

var users = xml.getElementsByTagName("match");
for (var i = 0; i < users.length; i++) {

  var names = users[i];
  for (j = 0; j < names.length; j++) {
    txt += "MATCH: " + " id: " + names[j].getAttribute('id') + " he: " + names[j].getAttribute('he') + " < br > ";
  }
}
document.getElementById("demo").innerHTML = txt;

This is my odds3.xml that has &.

<match id="32375537" he="Brighton & Hove Albion">

I expected the output to display my datas from odds3.xml, from & then replaced to and. Thanks for the help!

CONSOLE LOG: I added console.log(xml) but it returned "null" value, because the xml file have "&" on it. enter image description here

Expected output:

MATCH: id: 32375537 he: Brighton and Hove Albion
Angela
  • 81
  • 1
  • 10
  • Really need some help. Thanks! – Angela Oct 01 '19 at 08:00
  • Given your update, the problem is because you're calling `replace()` on the wrong thing. Remove `var xml = xml.replace(/&/g, "and");` completely, and just call replace on the `he` value itself: `" he: " + names[j].getAttribute('he').replace(/&/g, "and");` – Rory McCrossan Oct 01 '19 at 08:05
  • thanks i'll try it! @RoryMcCrossan – Angela Oct 01 '19 at 08:06
  • still not working, the xml file still cant be read because of "&" @RoryMcCrossan – Angela Oct 01 '19 at 08:09
  • `The browser is not reading the xml file` - if that were the case then `var xml = request.responseXML;` would be `null` – Jaromanda X Oct 01 '19 at 08:12

2 Answers2

1

1) yout request is synchrone, because you use send(...,FALSE). Asynchronous thing is not a issue in your case.

2) Check in the console with a console.log (xml). Do not display it in an html page and comment the replace line because the display of the content of this var can be changed in real time, at any time. And see if it is really a pur & in attribute.

var xml = request.responseXML;
/* var xml = xml.replace (/&/g, "and"); */
console.log (xml);

3) however: & is not valid in attribute xml, it must be replaced by &amp; during the production of the xml. Who produces this xml file? you or third party service, or another programmer?

4) replace the & is not a solution: imagine that later in the xml, you find a valid string

<text> here &amp; there </text>

it would become

<text> here and;amp; there </text>

5) try to work with responseText, not xmlResponse : this is the full raw response before browser tries to parse it.

var xhr = new XMLHttpRequest();
xhr.open('GET', '/ODDS/odds3.xml');
xhr.onload = function() {
    if (xhr.status === 200) {
        alert('response is' + xhr.responseText);
    }
    else {
        alert('Request failed.  Returned status of ' + xhr.status);
    }
};
xhr.send();
Eric
  • 608
  • 4
  • 11
  • a third party service is currently providing the xml file that's why i can't change the "&" all the time. But i can replace it to "&amp" as you requested. Can you help me with the code? @Eric – Angela Oct 01 '19 at 09:27
  • @Angela : ok, can you make that :
    var xml = request.responseXML; /* var xml = xml.replace (/&/g, "and"); */ console.log (xml);
    and post a result ?
    – Eric Oct 01 '19 at 09:28
  • hi i posted the console log, i comment out the replace function and added console.log(xml), it returns null because the xml file have "&" on it @Eric – Angela Oct 01 '19 at 09:33
  • @Angela : I updated my answer: the problem is that you use responseXml instead of responseText. the browser tries to parse responseText to produce responseXml but it can not because xml is invalid! 1) Retrieve responseText, repair your attribute, then parse manually your repaired response. this is another job – Eric Oct 01 '19 at 09:45
  • Thank you so much @Eric, it helped a lot! i can now fetch my xml, i will just repair my attributes – Angela Oct 01 '19 at 09:51
  • @Angela : Thank you, but consider the parts 3 and 4 of my response ... and you did not answer the 3 ... the real solution is: if (! Valid (xml)) { alert ('contact the supplier of the xml file, it is not valid!'); } else {... treatment ...}. Have a good day :-) – Eric Oct 01 '19 at 10:00
  • 2
    @Angela You can also thank Saif, for his code on jsfiddle (@Saif : sorry saif, I did not see your post, while I wrote mine, you were faster than me ...) – Eric Oct 01 '19 at 10:10
1

Solution mentioned in the jsfiddle link. Replace character on responseText not responseXml. you can convert that xmltext to xmldocument after successful replacement of required characters.

http://jsfiddle.net/ogaq9ujw/2/ var response=request.responseText; var xmlText=response.replace('GM',"and");

saif iqbal
  • 219
  • 1
  • 9