Is there a way to tell fast-xml-parser how to parse xml tags?

1.8k Views Asked by At

I am parsing an XML with fast-xml-parser. And the result JSON looks like this:

{
    RecordingToken: '4dddcdf8-0a0b-42a7-ae6d-231d5d7b6c40',
    Source: {
      Name: 'camera 40',
      Location: 'New Recording - Source Location',
      Description: 'New Recording - Source Description',
      Address: 'http://XXX/onvif/device_service'
    },
    EarliestRecording: '2021-12-02T00:00:00.038235Z',
    LatestRecording: '2021-12-22T08:30:49.364918Z',
    Content: 'Recording',
    Track: [
      {
        TrackToken: '12160e7c-6d41-4eb4-820f-239179b3911a',
        TrackType: 'Video',
        Description: 'Video Track',
        DataFrom: '2021-12-08T00:00:00.036076Z',
        DataTo: '2021-12-22T08:30:49.364918Z'
      }
    ],
    RecordingStatus: 'Recording'
}

My issue is that this object is not compliant with TS naming convention I am using. All object properties need to have lowerCamelCase style.

Is there a way to tell fast-xml-parser that I want to change first letter for xml tags when parsed into JSON? ( RecordingToken -> recordingToken)

1

There are 1 best solutions below

3
On

Fast XML Parser supports transformTagName property function from v4.0.9. Using this property, you can transform the tag name in lower case.

const options = {
  transformTagName: (tagName) => tagName.toLowerCase()
};
const parser = new XMLParser(options);
const output = parser.parse(xmlDataStr);

disclaimer: I'm the author of FXP