fast-xml-parse returns only one of object instead of array in Nodejs

1.7k views Asked by At

I'm doing tracking functionality using some service. It provides response in XML format. To parse XML response I have decided to use fast-xml-parser package. But there is a problem:

When I get only one item in response, it works well, but if I have multiple like:

<xsd:events xmlns:xsd="http://somelink/xsd">
        <event id=“1234”>
          <packetCode>ABC123</packetCode>
          <eventCode>EVENT_CODE</eventCode>
          <eventDate>2020-12-03T14:34:09.000+02:00</eventDate>
          <stateCode>STATUS_CODE</stateCode>
          <eventSource zip=“123">Some place</eventSource>
        </event>
        <event id=“456”>
          <packetCode>DEF456</packetCode>
          <eventCode>EVENT_CODE</eventCode>
          <eventDate>2020-12-03T14:40:44.000+02:00</eventDate>
          <stateCode>STATUS_CODE</stateCode>
          <eventSource zip=“123">Some place</eventSource>
        </event>
      </xsd:events>

After

const xmlParser = require('fast-xml-parser');
xmlParser.parse(exampleXml);

I'm getting an object only with last element:

{
    'xsd:events': {
      event: {
        packetCode: 'DEF456',
        eventCode: 'EVENT_CODE',
        eventDate: '2020-12-03T14:40:44.000+02:00',
        stateCode: 'STATUS_CODE',
        eventSource: 'Some place',
      }
    }
  }

but I expect to get array of both events. Would be nice to make it look like:

{
    'xsd:events': [{/* event data */}, {{/* event data */}]
  }

What am i doing wrong, maybe some options needed oro something else? Looking for help and advices.

1

There are 1 answers

1
Mads Hansen On BEST ANSWER

The problem is that the attributes have curly quotes instead of ".

This is often the result of copy/paste from Word and MS Office products that sometimes change regular quotes to the "smart quotes" that are the directional curly. These are not the same as regular quotes and often cause issues with parsing, since they are multi-byte characters and if not encoded properly will get mangled and read as two separate characters.

Fix the XML to use regular quotes:

<xsd:events xmlns:xsd="http://somelink/xsd">
    <event id=“1234”>
      <packetCode>ABC123</packetCode>
      <eventCode>EVENT_CODE</eventCode>
      <eventDate>2020-12-03T14:34:09.000+02:00</eventDate>
      <stateCode>STATUS_CODE</stateCode>
      <eventSource zip="123">Some place</eventSource>
    </event>
    <event id=“456”>
      <packetCode>DEF456</packetCode>
      <eventCode>EVENT_CODE</eventCode>
      <eventDate>2020-12-03T14:40:44.000+02:00</eventDate>
      <stateCode>STATUS_CODE</stateCode>
      <eventSource zip="123">Some place</eventSource>
    </event>
  </xsd:events>

And it will generate the expected JSON with an array of two objects for event:

{
    "xsd:events": {
        "event": [
            {
                "packetCode": "ABC123",
                "eventCode": "EVENT_CODE",
                "eventDate": "2020-12-03T14:34:09.000+02:00",
                "stateCode": "STATUS_CODE",
                "eventSource": "Some place"
            },
            {
                "packetCode": "DEF456",
                "eventCode": "EVENT_CODE",
                "eventDate": "2020-12-03T14:40:44.000+02:00",
                "stateCode": "STATUS_CODE",
                "eventSource": "Some place"
            }
        ]
    }
}