Skip to content

Node.js script for converting XML to CSV with custom mapping.

License

Notifications You must be signed in to change notification settings

cvd2412/xml2csv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XML2CSV

Run

$ npm run start

Options

Option Description
outPutFileName (Optional) Name of the output file
filter (Optional) Filter items with function
xmlFilePath Path to xml file
columnDataWrapper Name of the xml tag wrapping column data
mapping See below

Mapping

Example usage

example.xml

<Items>
    <Item>
        <Id>1230</Id>
        <Name>Item 1</Name>
        <Url>//example.com/item-1</Url>
        <ItemGroups>
            <ItemGroup>
                <Id>123456</Id>
                <Name>Group 1</Name>
            </ItemGroup>
            ...
        </ItemGroups>
    </Item>
    ...
</Items>

config.ts

export default {
  xmlFilePath: './example.xml',
  columnDataWrapper: 'Item',

  filter: (item) => !!item.querySelector('Name')?.textContent,

  mapping: {
    Name: 'Title', // returns textContent of specified tag
    Url:'URL',
    ItemGroups: ['Groups', (item, index, array) => {
      return [...item.querySelectorAll('ItemGroup > Name')].map(name => name.textContent).join(',')
    }]
  }
}

config.ts

Output:

Title URL Groups
Item 1 //example.com/item-1 Group 1
Item 2 //example.com/item-2 Group 1,Group 2
Item 3 //example.com/item-3 Group 2,Group 3

About

Node.js script for converting XML to CSV with custom mapping.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published