Convert String to xml with javascript

Use the following function to convert string to xml in javascript

function StringtoXML(text){
	if (window.ActiveXObject){
                  var doc=new ActiveXObject('Microsoft.XMLDOM');
                  doc.async='false';
                  doc.loadXML(text);
                } else {
                  var parser=new DOMParser();
                  var doc=parser.parseFromString(text,'text/xml');
	}
	return doc;
}
//voorbeeld toepassing:
var doc = StringtoXML(text);




We can also replace the string with regex

var s = "the batter hit the ball with the bat";

// Replace "the" with "a".
var re = /the/g;
var result = s.replace(re, "a");
document.write(result);
// Output: a batter hit a ball with a bat

Leave a Reply

Your email address will not be published. Required fields are marked *