【问题标题】:how to convert xml file into csv file in javascript如何在javascript中将xml文件转换为csv文件
【发布时间】:2016-01-07 14:43:44
【问题描述】:
<?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 xml csv demandware


【解决方案1】:

当您使用服务器端 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 中的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-03
    • 2011-03-05
    • 2015-10-28
    • 2014-11-22
    • 2017-04-18
    • 2020-03-21
    • 2011-02-15
    相关资源
    最近更新 更多