想在JAVASCRIPT將取得的資料存成XML格式檔案.但JAVASCIPT無法存檔.
所以想用JAVA來存檔. 但測試都沒有反應.請問那裏有問題. 謝謝.
JAVASCRIPT程式
importPackage( Packages.org.apache.poi.hssf.usermodel );
importPackage( Packages.java.io );
out_txt +="<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tip='http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay'>\n";
out_txt +=" soapenv:Header/\n";
out_txt +=" soapenv:Body\n";
out_txt +=" tip:CreatePoDataRequest\n";
out_txt +=" tip:request\n";
out_txt +="</tip:request>\n";
out_txt +=" </tip:CreatePoDataRequest>\n";
out_txt +=" </soapenv:Body>\n";
out_txt +="</soapenv:Envelope>";
Client.addInfoLog("\n"+out_txt);
var sapject = new Packages.ext.write();
writexml = sapject.getout_txt(out_txt);
JAVA的程式.
package ext;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class write {
public static void main(String[] args) throws Exception{
String lib_out_txt = "ABC";
String result = getout_txt(lib_out_txt);
System.out.println("Result from getouttxt: " + result);
}
public static String getout_txt(String out_txt1) {
String data = out_txt1;
String fileName = "D:\\456.txt";
try {
FileWriter fileWriter = new FileWriter(fileName);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(data);
bufferedWriter.close();
System.out.println("Data written: " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
}
在JavaScript程式中,你需要將要存成XML格式的資料先儲存在一個變數中,然後將這個變數傳給Java程式來進行存檔。以下是修改後的程式碼:
在JavaScript程式中:
importPackage(Packages.ext);
var out_txt = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tip='http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay'>\n";
out_txt += "<soapenv:Header/>\n";
out_txt += "<soapenv:Body>\n";
out_txt += "<tip:CreatePoDataRequest>\n";
out_txt += "<tip:request>\n";
out_txt += "</tip:request>\n";
out_txt += "</tip:CreatePoDataRequest>\n";
out_txt += "</soapenv:Body>\n";
out_txt += "</soapenv:Envelope>";
Client.addInfoLog("\n" + out_txt);
var sapject = new Packages.ext.write();
writexml = sapject.getout_txt(out_txt);
在Java程式中:
package ext;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class write {
public static void main(String[] args) throws Exception{
String lib_out_txt = args[0]; // 從JavaScript程式傳入的參數
String result = getout_txt(lib_out_txt);
System.out.println("Result from getouttxt: " + result);
}
public static String getout_txt(String out_txt1) {
String data = out_txt1;
String fileName = "D:\\456.txt";
try {
FileWriter fileWriter = new FileWriter(fileName);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(data);
bufferedWriter.close();
System.out.println("Data written: " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
}
你需要確保你將Java程式儲存成一個獨立的檔案,例如write.java
。然後使用Java編譯器(例如javac)將它編譯成一個可執行的 .class 檔案。接著在JavaScript程式中,使用 Packages.ext.write()
來建立 ext.write
類別的物件,並呼叫 getout_txt()
方法來執行存檔動作。
另外,你也可以將Java程式修改成一個簡單地執行檔,而不是使用命令列參數。這樣一來,在主函式中直接使用實際的資料,而不需要從命令列參數中取得。當然,這要視你的需求而定。
感謝回覆.
在browser寫XML檔已經可以.但檔名也需要用函數傳遞. 請看看那裏要修改. 謝謝.
JAVASCRIPT程式
importPackage( Packages.java.io );
var out_txt = "";
var FormNo = Form.getValue("txtFormNo");
out_txt +="<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tip='http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay'>\n";
out_txt +=" soapenv:Header/\n";
out_txt +=" soapenv:Body\n";
out_txt +=" tip:CreatePoDataRequest\n";
out_txt +=" tip:request\n";
out_txt +="</tip:request>\n";
out_txt +=" </tip:CreatePoDataRequest>\n";
out_txt +=" </soapenv:Body>\n";
out_txt +="</soapenv:Envelope>";
var sapject = new Packages.ext1.sapwrite();
writexml = sapject.getout_txt(out_txt,FormNo);
JAVA程式
package ext1;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class sapwrite {
public static void main(String[] args) throws Exception {
String lib_out_txt = args[0]; // 從JavaScript程式傳入的參數
String fileName0 = args[0]; // 從JavaScript程式傳入的參數
String result = getout_txt(lib_out_txt, fileName0);
System.out.println("Result from getouttxt: " + result);
}
public static String getout_txt(String out_txt1, String fileName1) {
String data = out_txt1;
fileName1 = "D:\\"+fileName1+".XML";
try {
FileWriter fileWriter = new FileWriter(fileName1);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(data);
bufferedWriter.close();
System.out.println("Data written: " + fileName1);
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
}
在JavaScript程式中,你可以將檔名作為參數傳遞給Java程式,然後在Java程式中使用該參數來設定檔名。
var fileName = "filename.xml";
var writexml = sapject.getout_txt(out_txt, fileName);
console.log(writexml);
以下是修改後的程式碼:
JAVASCRIPT程式
importPackage(java.io);
var out_txt = "";
var FormNo = Form.getValue(txtFormNo);
var fileName = "filename.xml";
out_txt += "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tip='http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay'>\n";
out_txt += "<soapenv:Header/>\n";
out_txt += "<soapenv:Body>\n";
out_txt += "<tip:CreatePoDataRequest>\n";
out_txt += "<tip:request>\n";
out_txt += "</tip:request>\n";
out_txt += "</tip:CreatePoDataRequest>\n";
out_txt += "</soapenv:Body>\n";
out_txt += "</soapenv:Envelope>";
var sapject = new Packages.ext1.sapwrite();
var writexml = sapject.getout_txt(out_txt, fileName);
console.log(writexml);
java
public class sapwrite {
public static void main(String[] args) throws Exception {
String out_txt1 = args[0]; // 從JavaScript程式傳入的參數
String fileName1 = args[1]; // 從JavaScript程式傳入的參數
String result = getout_txt(out_txt1, fileName1);
System.out.println("Result from getout_txt: " + result);
}
java跟javascript不同編程可以直接這樣溝通...@@?