Subj : Re: E4X on DOM nodes or fragments To : netscape.public.mozilla.jseng From : torntrousers Date : Mon Mar 21 2005 02:48 pm "Satyanandam Gullapudi" wrote in message news:... > All the examples and documentation I see on E4X create a variable pointing > to a XML fragment with in the script or open a document from with in the > script. Is there any way to pass a Java DOM XML fragment object or Java DOM > Node object or Xcursor Java object from Java to Javascript runtime via the > scope bindings ? Hi Satyanandam, The code below shows how to pass a DOM or XmlObject to a function. Note, if you want to _get_ a DOM node from a script you'll need to be using XmlBeans V2. public class Test2 { public static void main(String[] args) { Context cx = Context.enter(); try { StringBuffer sb = new StringBuffer(); sb.append("function foo(xml) {\n"); sb.append("java.lang.System.out.println(typeof(xml));"); sb.append("java.lang.System.out.println(xml);"); sb.append("return xml;\n"); sb.append("}\n"); String script = sb.toString(); Scriptable scope = cx.initStandardObjects(null, true); Script compiledScript = cx.compileString(script,"bla",1,null); compiledScript.exec(cx, scope); Function foo = (Function) scope.get("foo", scope); String xml = ""; //XmlObject request = Factory.parse(xml); DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder .parse(new InputSource(new StringReader(xml))); XmlObject request = Factory.parse(doc); Object wRequest = cx.getWrapFactory().wrap( cx, scope, request, XmlObject.class); Object jsRequest = cx.newObject( scope, "XML", new Object[] { wRequest }); Object[] functionArgs = new Object[] { jsRequest }; Scriptable jsResult = (Scriptable) foo.call(cx, scope, scope, functionArgs); System.out.println("jsResult:\n" + jsResult); Wrapper wrap = (Wrapper) ScriptableObject .callMethod(jsResult, "getXmlObject", new Object[0]); XmlObject result = (XmlObject) wrap.unwrap(); System.out.println("result:\n" + result); } catch (Exception e) { throw new RuntimeException(e); } finally { Context.exit(); } } } .