Navigation Menu

Skip to content

Commit

Permalink
Added code to normalize tag names and wrote tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neopunisher committed Oct 7, 2012
1 parent 048f20b commit cd43a5d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -118,6 +118,7 @@ value})``. Possible options are:
* `explicitCharkey` (default: `false`)
* `trim` (default: `false`): Trim the whitespace at the beginning and end of
text nodes.
* `normalizeTags` (default: `false`): Normalize all tag names to lowercase.
* `normalize` (default: `false`): Trim whitespaces inside text nodes.
* `explicitRoot` (default: `true`): Set this if you want to get the root
node in the resulting object.
Expand Down
4 changes: 3 additions & 1 deletion lib/xml2js.js

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

6 changes: 2 additions & 4 deletions src/xml2js.coffee
Expand Up @@ -30,6 +30,7 @@ exports.defaults =
explicitCharkey: false
trim: false
normalize: false
normalizeTags: false
attrkey: "$"
charkey: "_"
explicitArray: true
Expand Down Expand Up @@ -94,17 +95,14 @@ class exports.Parser extends events.EventEmitter
obj[attrkey][key] = node.attributes[key]

# need a place to store the node name
obj["#name"] = node.name
obj["#name"] = if @options.normalizeTags then node.name.toLowerCase() else node.name
stack.push obj

@saxParser.onclosetag = =>
obj = stack.pop()
nodeName = obj["#name"]
delete obj["#name"]

if @options.normalizeTags
nodeName = nodeName.toLowerCase()

s = stack[stack.length - 1]
# remove the '#' key altogether if it's blank
if obj[charkey].match(/^\s*$/)
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/sample.xml
Expand Up @@ -23,6 +23,11 @@
<item><subitem>Foo.</subitem><subitem>Bar.</subitem></item>
</arraytest>
<emptytest/>
<tagcasetest>
<tAg>something</tAg>
<TAG>something else</TAG>
<tag>something third</tag>
</tagcasetest>
<ordertest>
<one>1</one>
<two>2</two>
Expand Down
7 changes: 6 additions & 1 deletion test/xml2js.test.coffee
Expand Up @@ -59,7 +59,8 @@ module.exports =
equ r.sample.listtest[0].item[0].subitem[2], 'Foo(3)'
equ r.sample.listtest[0].item[0].subitem[3], 'Foo(4)'
equ r.sample.listtest[0].item[1], 'Qux.'
equ r.sample.listtest[0].item[2], 'Quux.')
equ r.sample.listtest[0].item[2], 'Quux.'
equ r.sample.tagcasetest.length, 3)

'test parse with explicitCharkey': skeleton(explicitCharkey: true, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
Expand Down Expand Up @@ -122,6 +123,10 @@ module.exports =
'test invalid empty XML file': skeleton(__xmlString: ' ', (r) ->
equ r, null)

'test enabled normalizeTags': skeleton(normalizeTags: true, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
equ r.sample.tagcasetest.length, 1)

'test parse with custom char and attribute object keys': skeleton(attrkey: 'attrobj', charkey: 'charobj', (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
equ r.sample.chartest[0].attrobj.desc, 'Test for CHARs'
Expand Down

0 comments on commit cd43a5d

Please sign in to comment.