Skip to content

Commit

Permalink
Added char and attribute object keys as options
Browse files Browse the repository at this point in the history
  • Loading branch information
neopunisher committed Aug 23, 2011
1 parent 73f5913 commit 1a65608
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/xml2js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class exports.Parser extends events.EventEmitter
trim: true
# normalize implicates trimming, just so you know
normalize: true
# set default attribute object key
attrkey: "@"
# set default char object key
charkey: "#"
# overwrite them with the specified options, if any
options[key] = value for own key, value of opts

Expand All @@ -37,16 +41,18 @@ class exports.Parser extends events.EventEmitter
# setting this property by and is deprecated, yet still supported.
# better pass it as explicitCharkey option to the constructor
@EXPLICIT_CHARKEY = options.explicitCharkey
@ATTRKEY = options.attrkey
@CHARKEY = options.charkey
@resultObject = null
stack = []

@saxParser.onopentag = (node) =>
obj = {}
obj["#"] = ""
obj[@CHARKEY] = ""
for own key of node.attributes
if "@" not of obj
obj["@"] = {}
obj["@"][key] = node.attributes[key]
if @ATTRKEY not of obj
obj[@ATTRKEY] = {}
obj[@ATTRKEY][key] = node.attributes[key]

# need a place to store the node name
obj["#name"] = node.name
Expand All @@ -59,15 +65,15 @@ class exports.Parser extends events.EventEmitter

s = stack[stack.length - 1]
# remove the '#' key altogether if it's blank
if obj["#"].match(/^\s*$/)
delete obj["#"]
if obj[@CHARKEY].match(/^\s*$/)
delete obj[@CHARKEY]
else
obj["#"] = obj["#"].trim() if options.trim
obj["#"] = obj["#"].replace(/\s{2,}/g, " ").trim() if options.normalize
obj[@CHARKEY] = obj[@CHARKEY].trim() if options.trim
obj[@CHARKEY] = obj[@CHARKEY].replace(/\s{2,}/g, " ").trim() if options.normalize
# also do away with '#' key altogether, if there's no subkeys
# unless EXPLICIT_CHARKEY is set
if Object.keys(obj).length == 1 and "#" of obj and not @EXPLICIT_CHARKEY
obj = obj["#"]
if Object.keys(obj).length == 1 and @CHARKEY of obj and not @EXPLICIT_CHARKEY
obj = obj[@CHARKEY]

if options.emptyTag != undefined && isEmpty obj
obj = options.emptyTag
Expand Down Expand Up @@ -96,7 +102,7 @@ class exports.Parser extends events.EventEmitter
@saxParser.ontext = @saxParser.oncdata = (text) =>
s = stack[stack.length - 1]
if s
s["#"] += text
s[@CHARKEY] += text

parseString: (str) =>
@saxParser.write str.toString()

0 comments on commit 1a65608

Please sign in to comment.