GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Sunday, March 13, 2011

JS - Converting DOM To String

/**
 * Here's a code sample to convert xml string to dom, and xml dom
 * back to string;
 */

<h1>XML DOM</h1>
<script>
var text = '<?xml version=\"1.0\"?>';
text = text + '<data>';
text = text + '<name>Lady Gaga</name>';
text = text + '<city>New York</city>';
text = text + '</data>';

var parser = new DOMParser();

// convert text string to dom
var dom = parser.parseFromString(text,'text/xml');

// convert back dom to string of text
alert((new XMLSerializer()).serializeToString(dom));

</script>

Share/Bookmark

No comments:

Post a Comment