1 year ago

#354485

test-img

Felix van Hove

Typescript, xml2js, explicitArray - how to deal with child nodes

I use xml2js to translate one type of XML into an object in Typescript. With its explicitArray option, xml2js forces users to either always generate arrays for nodes (explicitArray=true), or make array generation depending on the number of elements of this type read from the XML (explicitArray=false): If more than one, it becomes an array.

In order to work with the resulting object, I'm implementing interfaces and constants (currently 200 lines, likely to double). It typically looks like this:

export interface Foo {
    apiVersion: string,
    bar: Bar[],
    label: string,
    menus: string[]
}

Up until now I've worked with the default, i.e. child nodes are always arrays, but this setting becomes tedious, when only about 20% of elements can actually appear several times in the XML. I'm still relatively new to Typescript. Can you recommend a setting of explicitArray?

Are there standard strategies to deal with this problem?

Or maybe I've overlooked something?

typescript

xml2js

0 Answers

Your Answer

Accepted video resources