<p>I am using Adobe Flex/Air here, but as far as I know this applies to all of JavaScript. I have come across this problem a few times, and there must be an easy solution out there!</p>

<p>Suppose I have the following XML (using e4x):</p>

<pre><code>var xml:XML = &lt;root&gt;&lt;example&gt;foo&lt;/example&gt;&lt;/root&gt;
</code></pre>

<p>I can change the contents of the example node using the following code:</p>

<pre><code>xml.example = "bar";
</code></pre>

<p>However, if I have this:</p>

<pre><code>var xml:XML = &lt;root&gt;foo&lt;/root&gt;
</code></pre>

<p>How do i change the contents of the root node?</p>

<pre><code>xml = "bar";
</code></pre>

<p>Obviously doesn't work as I'm attempting to assign a string to an XML object.</p>

[{'answer_id': 62165, 'author': 'Loren Segal', 'author_id': 6436, 'author_profile': 'https://Stackoverflow.com/users/6436', 'pm_score': 0, 'selected': False, 'text': '<p>If you\'re trying to change the root element of a document, you don\'t really need to-- just throw out the existing document and replace it. Alternatively, just wrap your  element in a more proper root element (you shouldn\'t be editing the root node anyway) and you\'d be set.</p>\n\n<p>Of course, that doesn\'t answer your question. There\'s an ugly JS hack that can do what you want, but bear in mind that it\'s likely far slower than doing the above. Anyway, here it is:</p>\n\n<pre><code>var xml = &lt;root&gt;foo&lt;/root&gt;; // &lt;/fix_syntax_highlighter&gt;\nvar parser = new DOMParser();\nvar serializer = new XMLSerializer();\n\n// Parse xml as DOM document\n// Must inject "&lt;root&gt;&lt;/root&gt;" wrapper because  \n// E4X\'s toString() method doesn\'t give it to us\n// Not sure if this is expected behaviour.. doesn\'t seem so to me.\nvar xmlDoc = parser.parseFromString("&lt;root&gt;" + \n  xml.toString() + "&lt;/root&gt;", "text/xml");\n\n// Make the change\nxmlDoc.documentElement.firstChild.nodeValue = "CHANGED";\n\n// Serialize back to string and then to E4X XML()\nxml = new XML(serializer.serializeToString(xmlDoc));\n</code></pre>\n\n<p>You can ignore the fix_syntax_highlighter comment.</p>\n'}, {'answer_id': 62926, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 4, 'selected': True, 'text': "<p>It seems you confuse variables for the values they contain. The assignment</p>\n\n<pre><code>node = textInput.text;\n</code></pre>\n\n<p>changes the value the <em>variable</em> <code>node</code> points to, it doesn't change anything with the object that <code>node</code> currently points to. To do what you want to do you can use the <code>setChildren</code> method of the <code>XML</code> class:</p>\n\n<pre><code>node.setChildren(textInput.text)\n</code></pre>\n"}, {'answer_id': 63002, 'author': 'Grokys', 'author_id': 6448, 'author_profile': 'https://Stackoverflow.com/users/6448', 'pm_score': 1, 'selected': False, 'text': '<p>Ah thank you Theo - indeed seems I was confused there. I think the root of the confustion came from the fact I was able to assign </p>\n\n<pre><code>textInput.text = node; \n</code></pre>\n\n<p>Which I now guess is just implicity calling XML.toString() to convert XML->String. setChildren() is what I was looking for.</p>\n'}]
['https://Stackoverflow.com/questions/62086', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6448/']