Sunday, January 29, 2006

E4X to DOM and Back Again

If you're like me, some of the time, you'll work in E4X. Other times, you'll use DOM methods. Occasionally you'll want to switch back and forth from one to the other. Here's how to do the roundtrip.

// create E4X XML object
e4x = <div>Hello World!</div>;

// serialize it
serialzed = e4x.toXMLString();

// create DOM object
dom =
( new DOMParser() ).parseFromString(
serialzed, 'application/xml' );

// make it into a string again
serialized = ( new XMLSerializer() ).serializeToString(dom);

// back to E4X XML object
e4x = new XML( serialized );

Eventually we can expect to see support for a syntax like e4x = new XML( dom ), but this has yet to be implemented in Firefox.


  1. Another work-around for the time being to avoid serializing E4X nodes to a string is to do direct E4X to DOM node translation.

  2. Our Pay for informative speech essays, customer access our term paper writing services knowing that they are assured of the best online essay writing services.

  3. I was looking for E4X to DOM, because, I make my work easy. Thanks for sharing complete information about how to do the roundtrip? Your post is informative and it has solved my issue. Dissertation Writing Service


  4. Thanks for your content…Please visit my site!
    Sydney Busses


Add a comment. Registration required because trolls.