Permalink
Browse files

Added code to normalize tag names and wrote tests

  • Loading branch information...
neopunisher committed Oct 7, 2012
1 parent 048f20b commit cd43a5ddaed4cd227561cab9a8c3fc7774f1d20d
Showing with 17 additions and 6 deletions.
  1. +1 −0 README.md
  2. +3 −1 lib/xml2js.js
  3. +2 −4 src/xml2js.coffee
  4. +5 −0 test/fixtures/sample.xml
  5. +6 −1 test/xml2js.test.coffee
@@ -118,6 +118,7 @@ value})``. Possible options are:
* `explicitCharkey` (default: `false`) * `explicitCharkey` (default: `false`)
* `trim` (default: `false`): Trim the whitespace at the beginning and end of * `trim` (default: `false`): Trim the whitespace at the beginning and end of
text nodes. text nodes.
* `normalizeTags` (default: `false`): Normalize all tag names to lowercase.
* `normalize` (default: `false`): Trim whitespaces inside text nodes. * `normalize` (default: `false`): Trim whitespaces inside text nodes.
* `explicitRoot` (default: `true`): Set this if you want to get the root * `explicitRoot` (default: `true`): Set this if you want to get the root
node in the resulting object. node in the resulting object.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -30,6 +30,7 @@ exports.defaults =
explicitCharkey: false explicitCharkey: false
trim: false trim: false
normalize: false normalize: false
normalizeTags: false
attrkey: "$" attrkey: "$"
charkey: "_" charkey: "_"
explicitArray: true explicitArray: true
@@ -94,17 +95,14 @@ class exports.Parser extends events.EventEmitter
obj[attrkey][key] = node.attributes[key] obj[attrkey][key] = node.attributes[key]
# need a place to store the node name # 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 stack.push obj
@saxParser.onclosetag = => @saxParser.onclosetag = =>
obj = stack.pop() obj = stack.pop()
nodeName = obj["#name"] nodeName = obj["#name"]
delete obj["#name"] delete obj["#name"]
if @options.normalizeTags
nodeName = nodeName.toLowerCase()
s = stack[stack.length - 1] s = stack[stack.length - 1]
# remove the '#' key altogether if it's blank # remove the '#' key altogether if it's blank
if obj[charkey].match(/^\s*$/) if obj[charkey].match(/^\s*$/)
View
@@ -23,6 +23,11 @@
<item><subitem>Foo.</subitem><subitem>Bar.</subitem></item> <item><subitem>Foo.</subitem><subitem>Bar.</subitem></item>
</arraytest> </arraytest>
<emptytest/> <emptytest/>
<tagcasetest>
<tAg>something</tAg>
<TAG>something else</TAG>
<tag>something third</tag>
</tagcasetest>
<ordertest> <ordertest>
<one>1</one> <one>1</one>
<two>2</two> <two>2</two>
View
@@ -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[2], 'Foo(3)'
equ r.sample.listtest[0].item[0].subitem[3], 'Foo(4)' 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[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) -> 'test parse with explicitCharkey': skeleton(explicitCharkey: true, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10 console.log 'Result object: ' + util.inspect r, false, 10
@@ -122,6 +123,10 @@ module.exports =
'test invalid empty XML file': skeleton(__xmlString: ' ', (r) -> 'test invalid empty XML file': skeleton(__xmlString: ' ', (r) ->
equ r, null) 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) -> 'test parse with custom char and attribute object keys': skeleton(attrkey: 'attrobj', charkey: 'charobj', (r) ->
console.log 'Result object: ' + util.inspect r, false, 10 console.log 'Result object: ' + util.inspect r, false, 10
equ r.sample.chartest[0].attrobj.desc, 'Test for CHARs' equ r.sample.chartest[0].attrobj.desc, 'Test for CHARs'

0 comments on commit cd43a5d

Please sign in to comment.