Skip to content

Commit

Permalink
Merge pull request #42 from raoulmillais/master
Browse files Browse the repository at this point in the history
Add support for merging attributes and child elements (instead of keying them off a child property)
  • Loading branch information
Leonidas-from-XIV committed Dec 19, 2011
2 parents 38c35af + b37ae64 commit 1e19c77
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 41 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -123,6 +123,9 @@ value})``. Possible options are:
true; otherwise an array is created only if there is more than one.
* `ignoreAttrs` (default: `false`): Ignore all XML attributes and only create
text nodes.
* `mergeAttrs` (default: `false`): Merge attributes and child elements as
properties of the parent, instead of keying attributes off a child
attribute object. This option is ignored if `ignoreAttrs` is `false`.

These default settings are for backward-compatibility (and might change in the
future). For the most 'clean' parsing, you should disable `normalize` and
Expand Down
80 changes: 41 additions & 39 deletions lib/xml2js.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/xml2js.coffee
Expand Up @@ -22,6 +22,9 @@ class exports.Parser extends events.EventEmitter
explicitArray: false
# ignore all attributes regardless
ignoreAttrs: false
# merge attributes and child elements onto parent object. this may
# cause collisions.
mergeAttrs: false
# overwrite them with the specified options, if any
@options[key] = value for own key, value of opts

Expand Down Expand Up @@ -61,9 +64,12 @@ class exports.Parser extends events.EventEmitter
obj[charkey] = ""
unless @options.ignoreAttrs
for own key of node.attributes
if attrkey not of obj
if attrkey not of obj and not @options.mergeAttrs
obj[attrkey] = {}
obj[attrkey][key] = node.attributes[key]
if @options.mergeAttrs
obj[key] = node.attributes[key]
else
obj[attrkey][key] = node.attributes[key]

# need a place to store the node name
obj["#name"] = node.name
Expand Down
16 changes: 16 additions & 0 deletions test/xml2js.test.coffee
Expand Up @@ -55,6 +55,22 @@ module.exports =
assert.equal r['listtest']['item'][1]['#'], 'Qux.'
assert.equal r['listtest']['item'][2]['#'], 'Quux.')

'test parse with mergeAttrs': skeleton(mergeAttrs: true, (r) ->
console.log 'Result object: ' + util.inspect(r, false, 10)
assert.equal r['chartest']['desc'], 'Test for CHARs'
assert.equal r['chartest']['#'], 'Character data here!'
assert.equal r['cdatatest']['desc'], 'Test for CDATA'
assert.equal r['cdatatest']['misc'], 'true'
assert.equal r['cdatatest']['#'], 'CDATA here!'
assert.equal r['nochartest']['desc'], 'No data'
assert.equal r['nochartest']['misc'], 'false'
assert.equal r['listtest']['item'][0]['#'], 'This is character data!'
assert.equal r['listtest']['item'][0]['subitem'][0], 'Foo(1)'
assert.equal r['listtest']['item'][0]['subitem'][1], 'Foo(2)'
assert.equal r['listtest']['item'][0]['subitem'][2], 'Foo(3)'
assert.equal r['listtest']['item'][0]['subitem'][3], 'Foo(4)'
assert.equal r['listtest']['item'][1], 'Qux.'
assert.equal r['listtest']['item'][2], 'Quux.')
'test default text handling': skeleton(undefined, (r) ->
assert.equal r['whitespacetest']['#'], 'Line One Line Two')

Expand Down

0 comments on commit 1e19c77

Please sign in to comment.