Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

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

       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

like image 393
Shree29 Avatar asked Jan 07 '16 14:01

Shree29


People also ask

How can I convert XML to CSV?

Open the XML file and select all the text by clicking Ctrl + A then copy it by clicking Ctrl+C. Open Notepad ++ and paste the text from the previous step. From the top menu, open the Language sub-menu, then select XML.

Can you parse XML in JavaScript?

XML is used as a common exchange format for data. In a JavaScript action you can parse and generate XML using the xml-js library. The first example parses XML content of a local variable into a JavaScript object structure.

Can we convert XML to JSON in JavaScript?

To convert XML text to JavaScript object, use xml2js() . To convert XML text to JSON text, use xml2json() .

How do I convert XML files?

Open the XML file by clicking the Office Button-> Open and searching for the file on the computer. Click on Office Button->Print and in the Print window select novaPDF from the drop-down list. Click OK then OK again in the Save PDF File As window and the XML will be converted.


1 Answers

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.

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

like image 89
Zlatin Zlatev Avatar answered Oct 10 '22 09:10

Zlatin Zlatev