Parse AJAX data containing XML nodes with namespaces
November 10, 2017
Say you use AJAX to pull in data with an XML node containing a namespace prefix and you want to parse the data from within that node.
For example, what if you have the following?
... itemdate=items[i]; // This is the <item> node on which you are iterating in the AJAX script itemdate=itemdate.toString(); // this makes sure I can do the string methods like replace() itemdate=itemdate.replace(/mc:EventDate/g,"mcEventDate"); // globally changing the node to a parser-friendly format itemdate=itemdate.replace(/\/mc:EventDate/g,"/mcEventDate");// escaped the closing / as I globally change the node to a parser-friendly format itemdate=itemdate.split("<mcEventDate>");// now parse without all that grief! itemdate=itemdate; itemdate=itemdate.toString(); itemdate=itemdate.split("</mcEventDate>");// now parse without all that grief! itemdate=itemdate; // Now I have the data originally contained in the mc:EventDate node ...
The bottom-line, simple solution: replace the namespace prefix node name with something parser-friendly. That's it!