Converting an xml to CSV in Apigee

Hi @Dino-at-Google

Can anyone share an example in apigee to convert from xml payload to CSV?

I was searching in google I found csv to json but did not get any specific example on xml to csv.

const xml2csv = require('xml2csv')

xml2csv(
{
 xmlPath: 'path/to/file.xml',
 csvPath: 'path/to/file.csv',
 rootXMLElement: 'Record',
 headerMap: [
['Name', 'name', 'string'],
['Age', 'age', 'integer'],
['Gender', 'gender', 'string'],
['Brother', 'brother', 'string', 'Siblings'],
['Sister', 'sister', 'string', 'Siblings']
]
},
function (err, info) {
console.log(err, info)
// Done!
}
)
0 2 608
2 REPLIES 2

I suggest that you break down the problem you are facing into smaller pieces.

You want to convert from XML to CSV in Apigee. There's no builtin policy for that in Apigee Edge. Given that, you need to look to one of the extensibility options. And Javascript is a good idea for that.

There are a couple good options for running JavaScript in Apigee Edge - JavaScript callout and a Hosted Target. But you don't need to decide that right now.

What I suggest is that you focus just on... how to convert XML to CSV, in JavaScript. Solve that problem. Then confront the problem of getting your JavaScript logic to run inside Apigee Edge, either in the JS callout or in a Hosted Target.

It looks like you've found an npm module "xml2csv". That sounds promising. I haven't looked at it but I suppose there is a README for that module, and maybe it contains example code. Give that a shot - build a standalone nodejs app that converts in the way you want. Then lift it into Apigee Edge.

You could use an XSLT policy to convert to csv and store it in a flow variable.

There are multiple examples available. For instance: https://stackoverflow.com/questions/33955938/xslt-to-convert-unknown-xml-to-csv