# occxml2csv

> occ xml 2 csv

Latest version **1.0.2** (published 2020-04-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install occxml2csv
pnpm add occxml2csv
yarn add occxml2csv
bun add occxml2csv
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-04-09 |
| First published | 2020-04-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 36.5 KB |
| Known vulnerabilities | 0 (+8 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | julien philippe |
| Maintainers | julien.philippe.occ |

## Links

- npm: https://www.npmjs.com/package/occxml2csv
- Repository: https://github.com/julienphilippe/xml2csv
- Homepage: https://github.com/julienphilippe/xml2csv#readme
- Issues: https://github.com/julienphilippe/xml2csv/issues
- npm.io page: https://npm.io/package/occxml2csv

## Dependencies (5)

- [sax](https://npm.io/package/sax.md) 1.2.4
- [debug](https://npm.io/package/debug.md) 4.1.1
- [dottie](https://npm.io/package/dottie.md) 2.0.2
- [lodash](https://npm.io/package/lodash.md) 4.17.15
- [fs-extra](https://npm.io/package/fs-extra.md) ^9.0.0

## Recent versions

- 1.0.2 (latest) — 2020-04-09
- 1.0.1 — 2020-04-09
- 1.0.0 — 2020-04-09

## README

# occxml2csv
[![Tymly Package](https://img.shields.io/badge/tymly-package-blue.svg)](https://tymly.io/)
[![npm (scoped)](https://img.shields.io/npm/v/@wmfs/occxml2csv.svg)](https://www.npmjs.com/package/@wmfs/occxml2csv)
[![CircleCI](https://circleci.com/gh/wmfs/occxml2csv.svg?style=svg)](https://circleci.com/gh/wmfs/occxml2csv)
[![codecov](https://codecov.io/gh/wmfs/occxml2csv/branch/master/graph/badge.svg)](https://codecov.io/gh/wmfs/occxml2csv)
[![CodeFactor](https://www.codefactor.io/repository/github/wmfs/occxml2csv/badge)](https://www.codefactor.io/repository/github/wmfs/occxml2csv)
[![Dependabot badge](https://img.shields.io/badge/Dependabot-active-brightgreen.svg)](https://dependabot.com/)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/wmfs/tymly/blob/master/packages/pg-concat/LICENSE)

> Takes an XML file and transforms it into a CSV file, based on format of the map you provide with options. 

## <a name="install"></a>Install
```bash
$ npm install occxml2csv --save
```


## <a name="usage"></a>Usage

### Use with callback

```javascript
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

```javascript
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

```javascript
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](#headerMap) section for more details.

### <a name="headerMap"></a>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


## <a name="test"></a>Testing


```bash
$ npm test
```


## <a name="license"></a>License
[MIT](https://github.com/wmfs/tymly/occxml2csv/blob/master/LICENSE)

---
_Source: https://npm.io/package/occxml2csv · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
