Leonidas-from-XIV/node-xml2js — pull request #156 by ylarom (2014-09-04 07:42:09+00:00)
Title: added explicitArrayProcessor

My specific case has to do with parsing iOS Xcode Storyboard XML, where tag names indicate whether the children will be single or multiple, e.g.

...

Using hard set explicitArray to 'true' requires accessing objects awkwardly:
storyboard.scenes[0].scene[0].objects[0].view[0]

Using hard set explicitArray to 'false' will make the code break in different scenarios, depending on whether the parent has a single child or multiple children:
storyboard.scenes[0].scene.objects // works when there's a single scene, breaks on multiple scenes
storyboard.scenes[0].scene[0].objects // ok when there are multiple scenes, breaks on single scenes

I added an option called explicitArrayProcessor that uses a function to determine whether a child element should be part in an array.
The function takes 2 parameters: the parent name and the child element

In my case I just check if the parent's name ends with an 's', and if so I assume child elements should be put in an array.

I think this can be useful in other scenarios as well.