How to Generate a String for DOM Nodes in JavaScript
Since I just struggled with this for a couple of hours, I figured I’d post it here for posterity.
If you want to generate the string representation of some XML in JavaScript, here’s how to do it (using the Prototype 1.6 syntax for element creation), courtesy of Captain’s Universe:
var contactsElement = new Element("contacts"); var evanElement = new Element("contact"); Element.insert(evanElement, new Element("name").update("Evan Series")); Element.insert(evanElement, new Element("blog", {'type': 'personal'}).update("http://www.evanseries.org")); Element.insert(evanElement, new Element("email", {'type': 'school'}).update('[email protected]')) Element.insert(contactsElement, evanElement); var zachElement = new Element("contact"); Element.insert(zachElement, new Element("name").update("Zach Paine")); Element.insert(zachElement, new Element("blog", {'type': 'personal'}).update("http://zachpaine.info")); Element.insert(contactsElement, zachElement); alert(new XMLSerializer().serializeToString(contactsElement));
I don’t know if this works with IE yet; I’ll update this post when I find out.
Tags: dom, javascript, xml, xmlserializer
Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks
(Trackback URL)