I couldn't run following code using JDK 6. It throws an exception:
Exception in thread "main" org.mozilla.javascript.EcmaError: ReferenceError: "XML" is not defined.
My environment is following: Ubuntu 11.04, JDK 6, Rhino 1.7R2 (also tested with Rhino 1.7R3)
String script = "function abc(x) {var dd = new XML();}";
Context context = Context.enter();
try {
ScriptableObject scope = context.initStandardObjects();
Scriptable that = context.newObject(scope);
Function fct = context.compileFunction(scope, script, "script", 1, null);
Object result = fct.call(context, scope, that, new Object[] {2});
}
catch(Exception e){
e.printStackTrace();
}
finally {
Context.exit();
}
I had the same problem → in Rhino shell E4X works perfectly, but in embedded mode – ReferenceError: "XML" is not defined.
It was problem with old Xerces, I had 2.6.2 in my classpath when run my app. When I update it to 2.11 problem is gone.
$ java -cp js.jar:xerces-2.6.2.jar org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 3 2011 05 09
js> var x = <foo/>;
js: uncaught JavaScript runtime exception: ReferenceError: "XML" is not defined.
and
$ java -cp js.jar org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 3 2011 05 09
js> var x = <foo/>;
js> x.toXMLString();
<foo/>
Looks like when Rhino find Xerces, it uses it, instead of own mechanism to parse XML (when I start Shell with js.jar only in classpath).