by John Brinkman
Created
September 22, 2010
Over the time that I’ve been writing blog entries I’ve made many references to E4X and have included lots of samples that make use of this technology. However I’ve recently come to understand that I probably should have done a better job of introducing E4X usage, since it isn’t necessarily well understood by the average form developer.
For those of you who are unaware, E4X is an extension to JavaScript (ECMAScipt) for manipulating XML. The reason it is not particularly well known among JavaScript developers is that few of the browsers have implemented E4X. The notable exception is Mozilla / Firefox. And since the JavaScript engine inside Acrobat/Reader is based on Mozilla, E4X can be used reliably in Acrobat/Reader. To learn more about E4X, here are some links you may find useful: http://www.xml.com/pub/a/2007/11/28/introducing-e4x.html http://www.ecma-international.org/publications/standards/Ecma-357.htm http://rephrase.net/days/07/06/e4x http://wso2.org/project/mashup/0.2/docs/e4xquickstart.htmlThe XMLData Object
Before E4X was invented, we had recognized the need to have generic XML processing in Acrobat / Reader. Consequently, we added the XMLData object to the Acrobat object model. However, E4X was introduced shortly afterwards, and we ended up with two XML processing engines in Acrobat/Reader. In case there is any doubt, the right answer is to use E4X. You should no longer use the XMLData object. The XMLData object remains in the product for backward compatibility, but it does not get enhanced, it does not get bug fixes. E4X is faster and uses less memory.Some Usage Notes
There are several common learning curve issues you might encounter when using E4X:Oddly, E4X refuses to process the xml processing instruction at the front of an XML fragment. If it’s there, you will get a syntax error: “xml is a reserved identifier”. The workaround is to remove leading processing instructions with a regular expression such as:
sXML = sXML.replace(/^[\s\S]*?(<[^\?!])/, “$1″);
