occxml2csv

1.0.2 • Public • Published

occxml2csv

Tymly Package npm (scoped) CircleCI codecov CodeFactor Dependabot badge Commitizen friendly JavaScript Style Guide license

Takes an XML file and transforms it into a CSV file, based on format of the map you provide with options.

Install

$ npm install occxml2csv --save

Usage

Use with callback

const occxml2csv = require('occxml2csv')

occxml2csv(
  {
    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!
  }
)

Use with Promises

const occxml2csv = require('occxml2csv')

occxml2csv(
  {
    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']
    ]
  },
)
.then(console.log)
.catch(console.log);

Use with async/await

const occxml2csv = require('occxml2csv')

try {
  const res = await occxml2csv(
    {
      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']
      ]
    },
  )
  console.log(res)
} catch(err) {
  console.log(err)
}

Input:

<People>
    <Person>
        <Name>Maggie</Name>
        <Age>3</Age>
        <Gender>Female</Gender>
        <Siblings>
            <Brother>Bart</Brother>
            <Sister>Lisa</Sister>
        </Siblings>
    </Person>
    <Person>
        <Name>Marge</Name>
        <Age>45</Age>
        <Gender>Female</Gender>
    </Person>
</People>

Output:

name, age, gender, brother, sister
"Maggie",3,"Female","Bart","Lisa"
"Marge",45,"Female",,

occxml2csv(options, callback)

Options

Property Type Notes
xmlPath string A path to the xml input file. Cannot be provide with xmlStream property.
xmlStream fs.ReadStream A readable stream from an xml file. Cannot be provide with xmlPath property.
csvPath string The path and filename of the generated CSV output file (note that any intermediate folders will be created). Cannot be provide with csvStream property.
csvStream fs.WriteStream A writeable stream for the generated CSV data. Cannot be provide with csvPath property.
rootXMLElement string The XML root tag for each record, element to split records on in XML file.
headerMap [array] See the Header Map section for more details.

options.headerMap

options.headerMap needs to be in the structure of:

[
    [xmlTag, csvHeader, type, parent],
    [xmlTag, csvHeader, type, parent],
    ...
]
  • xmlTag and csvHeader must be the related fields
  • type must be integer, date or string
  • parent is optional, must be the parent tag in format of the XML tag

Testing

$ npm test

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i occxml2csv

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

37.4 kB

Total Files

12

Last publish

Collaborators

  • julien.philippe.occ