Currently, I am working on implementing tracking functionality using a specific service that provides responses in XML format. For parsing the XML response, I have opted to utilize the fast-xml-parser
package.
However, I have encountered an issue:
Everything works smoothly when there is only one item in the response. But when multiple items are present, like in the following example:
<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 executing the code:
const xmlParser = require('fast-xml-parser');
xmlParser.parse(exampleXml);
I noticed that the resulting object only contains the data of the 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',
}
}
}
instead of having an array with both events as expected. Ideally, the output should resemble:
{
'xsd:events': [{/* event data */}, {{/* event data */}]
}
I am seeking assistance and advice on what might be going wrong, whether certain options are required or if something else needs to be adjusted. Your help and guidance would be greatly appreciated.