Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.88k stars 604 forks source link

Allow children to be objects instead of arrays #489

Open the1mills opened 5 years ago

the1mills commented 5 years ago

I have only played with this lib for a little while, but it's shame that it seems like every child has to be an array. For example:

<bookstore>
  <book>foo</book>
  <zoom>bar</zoom>
</bookstore>

so that will get parsed to:

{
  bookstore:{
    "$":{},
    "book":['foo'],
    "zoom":['bar']
  }
}

but it would be really cool, if there was a way to standardize the XML, so that if it assumes it's not a list, unless you explicitly tell it, so that the above would get parsed to:

{
  bookstore:{
    "$":{},
    "book": 'foo',
    "zoom": 'bar'
  }
}

so that if you did this:

<bookstore list="book,zoom">
  <book>foo</book>
  <zoom>bar</zoom>
  <dang>zzz</dang>
</bookstore>

it would get parsed to:

{
  bookstore:{
    "$":{},
    "book": ['foo'],
    "zoom": ['bar'],
    "dang": 'zzz'
  }
}
Leonidas-from-XIV commented 5 years ago

If you set explicitArray to false it will do that. I recommend against it because it will make your code fragile since the fact whether it is an array or not depends on the specific XML (and not any schema), so code might break when there is suddenly just one child node or when instead of one there are multiple.

the1mills commented 5 years ago

@Leonidas-from-XIV yep I agree, I am trying to think of a generic solution, but yeah there might not be one. If you don't control the XML that comes to you, or if it could change, then you have to handle the format that it's in. Personally, I consider this a faulty design on the part of XML.

the1mills commented 5 years ago

@Leonidas-from-XIV this is what I am wondering - https://gist.github.com/ORESoftware/c917af15b1e5bce9617e1ebcec780554

maybe XML has a newer standard that will fix this. It's crazy.

ALCarden commented 5 years ago

@Leonidas-from-XIV this is what I am wondering - https://gist.github.com/ORESoftware/c917af15b1e5bce9617e1ebcec780554

maybe XML has a newer standard that will fix this. It's crazy.

There is a standard, it's just in the javascript world everyone seems to forget things called schemas. Proper XML tools in most languages when doing parsing, code gen etc will use and respect a schema.

The schema can tell you if a child is a list via the schema properties, minOccurs, maxOccurs, mustOccurs, e.g. if minOccurs and maxOccurs = 1 then its a single element not a list, if it had minOccurs=0 and no maxOccurs then it would be optional list

Leonidas-from-XIV commented 5 years ago

I am aware of the existence of schemas. But while sometimes you have a schema and can apply it often you just get some XML so I'm parsing it in the most general way.

I am not sure it is even worth implementing support for this given the low amount of XML Schema in the wild.

smandlavdiya commented 3 years ago

@Leonidas-from-XIV do we have an option to add an attribute to all the list when we build the xml from json example array=true for all the list irrespective of number of elements? and can use same attribute to make it array when parse json from same xml