Subj : Re: Rhino DOM integration. To : Bruno Vignola From : Igor Bukanov Date : Thu Jan 08 2004 12:42 pm Bruno Vignola wrote: > my Java application embeds rhino for executing js scripts; the > application gets a XML document, I can parse it, i.e. using the > Microsoft parser, then I would like to access the document using > the DOM interfaces inside the rhino environment, e.g. I would > like write in my JS scripts something like: > > ... > value = x.childnodes(0).nodeValue etc. etc. > Suppose your have an instance of org.w3c.dom.Document interface, then consider a modified version of example/RunScript2.java from Rhino distribution : import org.mozilla.javascript.*; public class RunScript2 { public static void main(String args[]) throws Exception { org.w3c.dom.Document document = parseXmlDocumentFromFile(args[0]); Context cx = Context.enter(); try { Scriptable scope = cx.initStandardObjects(); // Add a global variable "out" that is a JavaScript // reflection of System.out Scriptable jsArgs = Context.toObject(System.out, scope); scope.put("out", scope, jsArgs); // Add a global variable "document" that is a JavaScript // reflection of org.w3c.dom.Document Scriptable jsArgs = Context.toObject(document, scope); scope.put("document", scope, jsArgs); String script = args[1]; Object result = cx.evaluateString(scope, script, "", 1, null); System.err.println(cx.toString(result)); } finally { Context.exit(); } } org.w3c.dom.Document parseXmlDocumentFromFile(String fileName) throws Exception { // read and parse the given file into W3C document } } Now run the example with: java RunScripr2 x.xml 'document.getElementsByTagName("a").length' where x.xml contains: It should print 4, the number of "a" tags in the document. Regards, Igor .