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();}}文件內(nèi)容和上面一樣 。
3 Rhino JavaScript 引擎介紹默認(rèn)的 Nashorn 引擎是無法解析 xml 的,像 DOMParser 這樣的對象是瀏覽器內(nèi)置的組件 。
這里可以通過 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 文件內(nèi)容如下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("----------------------------------------");執(zhí)行如下@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 內(nèi)容如下<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();}}上面的輸出結(jié)果如下
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助:- js腳本編寫教程 js網(wǎng)頁編程
- 免費做視頻特效的軟件推薦 ppt特效制作教程
- dnf立繪補丁教程 dnf美化包怎么設(shè)置
- 安裝windows10步驟 windows重裝系統(tǒng)教程
- 筆記本win10優(yōu)化代碼 win優(yōu)化設(shè)置教程
- 阿里云網(wǎng)站搭建教程 阿里云搭建網(wǎng)站多少錢
- 附操作流程圖文教程 阿里云注冊域名的步驟
- 服務(wù)器虛擬化平臺搭建 虛擬機搭建服務(wù)器教程
- 蘋果6強刷ios13教程 蘋果手機版本更新要多久
- idea創(chuàng)建桌面快捷方式 ubuntu安裝idea教程
