1.32.0 • Published 10 months ago

@wmfs/xml2csv v1.32.0

Weekly downloads
150
License
MIT
Repository
github
Last release
10 months ago

xml2csv

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 @wmfs/xml2csv --save

Usage

Use with callback

const xml2csv = require('@wmfs/xml2csv')

xml2csv(
  {
    xmlPath: 'path/to/file.xml',
    csvPath: 'path/to/file.csv',
    rootXMLElement: 'Person',
    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 xml2csv = require('@wmfs/xml2csv')

xml2csv(
  {
    xmlPath: 'path/to/file.xml',
    csvPath: 'path/to/file.csv',
    rootXMLElement: 'Person',
    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 xml2csv = require('@wmfs/xml2csv')

try {
  const res = await xml2csv(
    {
      xmlPath: 'path/to/file.xml',
      csvPath: 'path/to/file.csv',
      rootXMLElement: 'Person',
      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",,

xml2csv(options, callback)

Options

PropertyTypeNotes
xmlPathstringA path to the xml input file. Cannot be provide with xmlStream property.
xmlStreamfs.ReadStreamA readable stream from an xml file. Cannot be provide with xmlPath property.
csvPathstringThe path and filename of the generated CSV output file (note that any intermediate folders will be created). Cannot be provide with csvStream property.
csvStreamfs.WriteStreamA writeable stream for the generated CSV data. Cannot be provide with csvPath property.
rootXMLElementstringThe XML root tag for each record, element to split records on in XML file.
headerMap[array]See the Header Map section for more details.
strict[boolean]Should the sax parser run in strict mode ? Defaults to true.

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

1.32.0

10 months ago

1.31.0

11 months ago

1.29.0

1 year ago

1.30.0

1 year ago

1.27.0

2 years ago

1.28.0

2 years ago

1.25.0

2 years ago

1.26.0

2 years ago

1.24.0

3 years ago

1.23.0

3 years ago

1.22.0

3 years ago

1.21.0

3 years ago

1.20.0

4 years ago

1.19.0

4 years ago

1.18.0

4 years ago

1.17.0

4 years ago

1.16.0

4 years ago

1.15.0

4 years ago

1.14.0

4 years ago

1.13.0

4 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.0

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago