如何在javascript中将xml文件转换为csv文件

时间:2022-05-24 21:46:09
<?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>

I am trying to convert this xml into csv I have tried some thing like this but I am not getting any logic to convert data to csv format

我试图将此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;
        }

But I don't know how I can convert xml data into strings of array

但我不知道如何将xml数据转换为数组字符串

1 个解决方案

#1


4  

As you're using server-side JavaScript, moreover DemandwareScript, check XMLStreamReader class and E4X syntax. https://en.m.wikipedia.org/wiki/ECMAScript_for_XML E4X is EcmaScript For Xml, a proprietary syntax which is used for XML DOM access in Demandware. You would need one more File object for reading the XML file.

当您使用服务器端JavaScript,以及DemandwareScript时,请检查XMLStreamReader类和E4X语法。 https://en.m.wikipedia.org/wiki/ECMAScript_for_XML E4X是EcmaScript For Xml,一种专有语法,用于Demandware中的XML DOM访问。您还需要一个File对象来读取XML文件。

Also your XML file is not "flat", so you would need to define which XML elements go where in the CSV.

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

#1


4  

As you're using server-side JavaScript, moreover DemandwareScript, check XMLStreamReader class and E4X syntax. https://en.m.wikipedia.org/wiki/ECMAScript_for_XML E4X is EcmaScript For Xml, a proprietary syntax which is used for XML DOM access in Demandware. You would need one more File object for reading the XML file.

当您使用服务器端JavaScript,以及DemandwareScript时,请检查XMLStreamReader类和E4X语法。 https://en.m.wikipedia.org/wiki/ECMAScript_for_XML E4X是EcmaScript For Xml,一种专有语法,用于Demandware中的XML DOM访问。您还需要一个File对象来读取XML文件。

Also your XML file is not "flat", so you would need to define which XML elements go where in the CSV.

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