iT邦幫忙

0

JAVASCRIPT 如何讀取及產出XML檔案

  • 分享至 

  • xImage

想在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;
}

}

knvbhk iT邦新手 5 級 ‧ 2023-11-01 17:11:11 檢舉
Javascript 和 Java 是兩碼子的事,不要混淆
knvbhk iT邦新手 5 級 ‧ 2023-11-01 17:15:01 檢舉
另外,請問你的javascript 是在browser 中執行還是在command line 中執行?不同執行環境solution 不一樣。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
rain_yu
iT邦新手 1 級 ‧ 2023-11-02 09:04:22
最佳解答

在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程式修改成一個簡單地執行檔,而不是使用命令列參數。這樣一來,在主函式中直接使用實際的資料,而不需要從命令列參數中取得。當然,這要視你的需求而定。

看更多先前的回應...收起先前的回應...
ellon iT邦新手 5 級 ‧ 2023-11-02 13:04:52 檢舉

感謝回覆.

ellon iT邦新手 5 級 ‧ 2023-11-02 13:11:48 檢舉

在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;
}

}

rain_yu iT邦新手 1 級 ‧ 2023-11-03 08:39:20 檢舉

在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);
    }
a05151988 iT邦新手 3 級 ‧ 2023-11-03 09:36:22 檢舉

java跟javascript不同編程可以直接這樣溝通...@@?

rain_yu iT邦新手 1 級 ‧ 2023-11-03 10:46:41 檢舉

看開發工具吧,例如.net core,用vs
就是C#和js互相傳值

ellon iT邦新手 5 級 ‧ 2023-11-03 16:27:10 檢舉

依照rain_yu 提供的來做. 編譯成class.
java spawrite "abc" "def" .可以正確認產生檔案及內容.
JAVASCRIPT 沒有執行這行.var writexml = sapject.getout_txt(out_txt, fileName);

rain_yu iT邦新手 1 級 ‧ 2023-11-06 08:40:56 檢舉

有成功了嗎?

我要發表回答

立即登入回答