Xml to csv in javascript

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Convert xml to csv in streaming fashion

License

prabhay759/xml-csv

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.MD

xml version="1.0" encoding="utf-8"?> ArrayOfRoute> Route> Iso2>ADIso2> ZipFrom>60000ZipFrom> ZipTo>60005ZipTo> Agent>6000Agent> Ruta>Ruta> Color>VERDEColor> AvailableService>1110000AvailableService> Airport>1110000Airport> Route> Route> Iso2>ADIso2> ZipFrom>60002ZipFrom> ZipTo>60001ZipTo> Agent>6000Agent> Ruta>2ARuta> Color>VERDEColor> AvailableService>1110000AvailableService> Airport>1110000Airport> Route> ArrayOfRoute>
const fs = require("fs"); const xmlcsv = require("xml-csv"); xmlcsv( source: fs.createReadStream("./sample.xml"), rootXMLElement: "Route", headerMap: [ ["Iso2", "ISO2", "string"], ["ZipFrom", "ZIP", "string"], ["Agent", "AgentCode", "string"], ["Ruta", "Route", "string"], ["Color", "Color", "string"], ["AvailableService", "AvailableService", "string"], ["Airport", "GatewayAirport", "string"] ] >).pipe(fs.createWriteStream("./sample_1.csv"));
ISO2,ZIP,AgentCode,Route,Color,AvailableService,GatewayAirport AD,60000,6000,,VERDE,1110000,1110000 AD,60002,6000,2A,VERDE,1110000,1110000 

Источник

Nodejs, How to Convert XML to CSV| Javascript

In Javascript, there is no direct solution to convert XML to JSON, We have npm library @wmfs/xml2csv . You can also check other posts on npm command deprecate option is deprecated First, Let’s create a nodejs application using the npm init command

This command creates a nodejs application with all default values

Next, Install the @wmfs/xml2csv dependency to an application using the below command

This adds dependency in package.json as follows

 "dependencies":  "@wmfs/xml2csv": "^1.23.0", >

Let’s declare XML file an for this example

   12 john admin false 2000-10-01  

How to Convert XML to CSV in Nodejs

  • First import module @wmfs/xml2csv and loads module and creates an object xml2csv
  • Create an xml2csv object with options object and callback function

Options object contains the following properties

  • xmlPath : input XML path location and XML file name
  • csvPath : output csv path location
  • rootXMLElement : root tag for each XML record to read, In our example user.xml file, We have a user tag that contains data
  • headerMap : mapping xml tag to csv columns HeaderMap declaration contains the field definition
[xmlTag, csvHeader, type, parent],

xmlTag: XML tag to be converted to CSV fields i.e id,username,role,active

csvHeader: xsv content fields

type: type of the field date, string, and number

parent: Optional parent tag of the XML tag

Call back example to convert XML to CSV

The callback function is called with the first parameter as an error and the second parameter result.

Example for converting XML to CSV with asynchronous operation

const xml2csv = require("@wmfs/xml2csv"); xml2csv(  xmlPath: "user.xml", csvPath: "csvfile.csv", rootXMLElement: "user", headerMap: [ ["id", "id", "integer"], ["username", "username", "string"], ["role", "role", "string"], ["active", "active", "boolean"], ], >, function (err, result)  console.log(err); console.log(result); > );

Output: csvfile.csv file is created with the following data

id,username,role,active 12,"john","admin",false

Promise example to convert xml to csv in nodejs

Promise.then contains two functions, the First function is executed with a resolved promise and the second function is called with a rejected case Promise.catch contains the error function called for promise error Here is Syntax

promise.then( function(result)  // Calls success result >, function(error)  // calls an error case > ).catch(function(promiseerror) // error case >);

The same above can be rewritten, replace lambda expressions and simplify the code clean.

promise .then( (result) =>  console.log("XML to CSV conversion completed", result); >, (error) =>  console.log("XML to CSV conversion rejected", error); > ) .catch((error) =>  // error case >);

Here is an example with a simplified version You can check other posts on How to always run some code when a promise is resolved or rejected xml2csvPromise.js:

const xml2csv = require(«@wmfs/xml2csv»); xml2csv( xmlPath: ‘user.xml’, csvPath: ‘csvfile.csv’, rootXMLElement: ‘user’, headerMap: [ [‘id’, ‘id’, ‘integer’], [‘username’, ‘username’, ‘string’], [‘role’, ‘role’, ‘string’], [‘active’, ‘active’, ‘boolean’], ] >,).then((result)=> console.log(«XML to CSV conversion completed»,result); >,(err=> console.log(«error conversion rejected»,err); >)) .catch((err)=> console.log(«Conversion Failed»,err); >));

Conclusion

Learned multiple ways to convert xml to CSV with examples.

Источник

Xml to csv in javascript

xml version="1.0" encoding="utf-8"?>
ArrayOfRoute>
Route>
Iso2>ADIso2>
ZipFrom>60000ZipFrom>
ZipTo>60005ZipTo>
Agent>6000Agent>
Ruta>/Ruta>
Color>VERDEColor>
AvailableService>1110000AvailableService>
Airport>1110000Airport>
Route>
Route>
Iso2>ADIso2>
ZipFrom>60002ZipFrom>
ZipTo>60001ZipTo>
Agent>6000Agent>
Ruta>2ARuta>
Color>VERDEColor>
AvailableService>1110000AvailableService>
Airport>1110000Airport>
Route>
ArrayOfRoute>
const fs = require("fs");
const xmlcsv = require("xml-csv");
xmlcsv(
source: fs.createReadStream("./sample.xml"),
rootXMLElement: "Route",
headerMap: [
["Iso2", "ISO2", "string"],
["ZipFrom", "ZIP", "string"],
["Agent", "AgentCode", "string"],
["Ruta", "Route", "string"],
["Color", "Color", "string"],
["AvailableService", "AvailableService", "string"],
["Airport", "GatewayAirport", "string"]
]
>).pipe(fs.createWriteStream("./sample_1.csv"));
ISO2,ZIP,AgentCode,Route,Color,AvailableService,GatewayAirport
AD,60000,6000,,VERDE,1110000,1110000
AD,60002,6000,2A,VERDE,1110000,1110000

Источник

Читайте также:  Css styling using class
Оцените статью