簡體   English   中英

如何在javascript中將xml文件轉換為csv文件

[英]how to convert xml file into csv file in javascript

<?xml version="1.0" encoding="UTF-8"?>
<current>
   <city id="1259229" name="Pune">
      <coord lon="73.86" lat="18.52" />
      <country>IN</country>
      <sun rise="2016-01-07T01:38:29" set="2016-01-07T12:42:53" />
   </city>
   <temperature value="27.49" min="27.49" max="27.49" unit="metric" />
   <humidity value="43" unit="%" />
   <pressure value="955.13" unit="hPa" />
   <wind>
      <speed value="2.65" name="Light breeze" />
      <gusts />
      <direction value="113.502" code="ESE" name="East-southeast" />
   </wind>
   <clouds value="36" name="scattered clouds" />
   <visibility />
   <precipitation mode="no" />
   <weather number="802" value="scattered clouds" icon="03d" />
   <lastupdate value="2016-01-07T06:25:45" />
</current>

我正在嘗試將此xml轉換為csv,我已經嘗試過類似的操作,但是我沒有任何邏輯將數據轉換為csv格式

       try {
            var file : File = new File(dw.io.File.IMPEX + '/src/weather.csv');
            var fileWriter : FileWriter = new FileWriter(file, 'UTF-8');
            var csw : CSVStreamWriter = new CSVStreamWriter(fileWriter);
            csw.writeNext(//here I want array of string data );
            csw.writeEndDocument();
            csw.close();
            fileWriter.close();
        }
        catch(e) {
            return PIPELET_ERROR;
        }

但是我不知道如何將xml數據轉換為數組字符串

此外,在使用服務器端JavaScript時,請使用DemandwareScript,檢查XMLStreamReader類和E4X語法。 https://en.m.wikipedia.org/wiki/ECMAScript_for_XML E4X是EcmaScript For Xml,一種專用語法,用於Demandware中的XML DOM訪問。 您將需要一個File對象來讀取XML文件。

同樣,您的XML文件也不是“平面”的,因此您需要定義哪些XML元素在CSV中的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM