js脚本编写教程 javascript脚本怎么运行( 二 )


src/test/resources/test.js@Testpublic void test_invoke_js_file() {try {ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");engine.eval(new FileReader(TestJSEngine.class.getResource("/test.js").getPath()));Invocable invocable = (Invocable) engine;Object result = invocable.invokeFunction("checkSign", null);System.out.println(result);result = invocable.invokeFunction("getData", null);System.out.println(result);result = invocable.invokeFunction("calculate", 2, 5);System.out.println(result);} catch (Exception e) {e.printStackTrace();}}文件内容和上面一样 。
3 Rhino JavaScript 引擎介绍默认的 Nashorn 引擎是无法解析 xml 的,像 DOMParser 这样的对象是浏览器内置的组件 。
这里可以通过 Maven 依赖 Rhino 引擎来处理 xml 。
Rhino Maven 依赖如下<dependency><groupId>cat.inspiracio</groupId><artifactId>rhino-js-engine</artifactId><version>1.7.10</version></dependency>使用的步骤和其他 JavaScript 引擎一样,引擎的名称为 Rhino.
3.1 Rhino 对 xml 的解析这里通过读取文件的方式来加载和解析 JavaScript 脚本,脚本中是对一段 xml 的解析的过程 。
src/test/resources/xml.js 文件内容如下print("----------------------------------------");var e = new XML('<employees> <employee id="1"><name>Joe</name><age>20</age></employee> <employee id="2"><name>Sue</name><age>30</age></employee></employees>');// 获取所有的员工print("获取所有的员工:\n" + e..name);// 名字叫 Joe 的员工print("名字叫 Joe 的员工:\n" + e.employee.(name == "Joe"));// 员工的id 为 1 和 2print("员工的id 为 1 和 2:\n" + e.employee.(@id == 1 || @id == 2));// 员工的id 为 1print("员工的id 为 1: " + e.employee.(@id == 1).name);print("----------------------------------------");执行如下@Testpublic void test_rhino_file_js() {try {ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");ScriptContext scriptContext = engine.getContext();StringWriter stringWriter = new StringWriter();PrintWriter printWriter = new PrintWriter(stringWriter);scriptContext.setWriter(printWriter);engine.eval(new FileReader(TestJSEngine.class.getResource("/xml.js").getPath()));System.out.println(String.format("xml result = %s",stringWriter.toString() ));} catch (Exception e) {e.printStackTrace();}}输出如下xml result = ----------------------------------------All the employee names are:<name>Joe</name><name>Sue</name>The employee named Joe is:<employee id="1"><name>Joe</name><age>20</age></employee>Employees with ids 1 & 2:<employee id="1"><name>Joe</name><age>20</age></employee><employee id="2"><name>Sue</name><age>30</age></employee>Name of the the employee with ID=1: Joe----------------------------------------3.2 测试xml 内容如下<CCardProcessSyncResponse><RetCode>0</RetCode><Message>操作成功!</Message><RefundCycle /><EpayRefundCycleMin>1</EpayRefundCycleMin><EpayRefundCycleMax>7</EpayRefundCycleMax><EpayRefundCycleUnitF /></CCardProcessSyncResponse>具体测试如下@Testpublic void test_rhino() {try {ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");String jsString = jsString = "var obj=new XML('<CCardProcessSyncResponse><RetCode>0</RetCode><Message>操作成功!</Message><RefundCycle /><EpayRefundCycleMin>1</EpayRefundCycleMin><EpayRefundCycleMax>7</EpayRefundCycleMax><EpayRefundCycleUnitF /></CCardProcessSyncResponse>');print(obj.Message == '操作成功!');";ScriptContext scriptContext = engine.getContext();StringWriter stringWriter = new StringWriter();PrintWriter printWriter = new PrintWriter(stringWriter);scriptContext.setWriter(printWriter);engine.eval(jsString);System.out.println(String.format("xml result = %s",stringWriter.toString() ));} catch (Exception e) {e.printStackTrace();}}上面的输出结果如下


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: