0

我有以下从 java 运行的 javascript 代码:

var doc = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html',  null);
doc.documentElement.innerHTML = xmlResponse;

我收到以下错误:

Caused by: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "document" is not defined. (<Unknown source>#13)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3773)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3751)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.notFoundError(ScriptRuntime.java:3836)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.name(ScriptRuntime.java:1762)
    at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1790)
    at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:854)
    at sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:164)
    at sun.org.mozilla.javascript.internal.ContextFactory.doTopCall(ContextFactory.java:429)
    at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:116)
    at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:109)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:3163)
    at sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:175)
    at sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1159)
    at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:210)
    ... 16 more

你知道如何解决它吗?

4

1 回答 1

0

根据文档

window 对象表示浏览器中打开的窗口。

由于您没有在浏览器中执行脚本,因此未定义 window 对象。

您可以使用 URL/URLConnecion 类读取 URL,并将其提供给 ScriptEngine。这里有一个教程

于 2012-10-17T07:12:57.343 回答