russplaysguitar / UnderscoreCF

An UnderscoreJS port for Coldfusion. Functional programming library.
http://russplaysguitar.github.com/UnderscoreCF/
MIT License
89 stars 38 forks source link

Add toXml(collection, [*elementNames]) #28

Closed russplaysguitar closed 11 years ago

russplaysguitar commented 11 years ago

There are a lot of different ways to convert json to xml, but the goal here is to produce the simplest xml possible given the particular collection.

XML element names would default to the type of collection. The elementNames arguments offer a way to name the elements at levels without keys (arrays). See the examples below for details.

There is no json representation for xml attributes.

If more customized xml is needed, then an iterative method (like _.each()) should be used instead.

Examples

Arrays:

_.toXml([1, 2, 3]) =>
<array>
  <element>1</element>
  <element>2</element>
  <element>3</element>
</array>

_.toXml([1, 2, 3], 'numbers') =>
<numbers>
  <element>1</element>
  <element>2</element>
  <element>3</element>
</numbers>

_.toXml([1, 2, 3], 'numbers', 'number') =>
<numbers>
  <number>1</number>
  <number>2</number>
  <number>3</number>
</numbers>

Structs:

_.toXml({one: 1, two: 2, three: 3}) =>
<struct>
  <one>1</one>
  <two>2</two>
  <three>3</three>
</struct>

_.toXml({one: 1, two: 2, three: 3}, 'numbers') =>
<numbers>
  <one>1</one>
  <two>2</two>
  <three>3</three>
</numbers>

Arrays of Structs:

_.toXml([{one: 1, two: 2, three: 3}, {four: 4, five: 5, six: 6}]) =>
<array>
  <struct>
    <one>1</one>
    <two>2</two>
    <three>3</three>
  </struct>
  <struct>
    <four>4</four>
    <five>5</five>
    <six>6</six>
  </struct>
</array>

_.toXml([{one: 1, two: 2, three: 3}, {four: 4, five: 5, six: 6}], 'items', 'numbers') =>
<items>
  <numbers>
    <one>1</one>
    <two>2</two>
    <three>3</three>
  </numbers>
  <numbers>
    <four>4</four>
    <five>5</five>
    <six>6</six>
  </numbers>
</items>