@@ -51,34 +51,34 @@ function startElement(name, attrs) {
5151}
5252
5353function text ( data ) {
54- currentObject [ '$t' ] = ( currentObject [ '$t' ] || '' ) + data ;
54+ currentObject [ textNodeName ( ) ] = ( currentObject [ textNodeName ( ) ] || '' ) + data ;
5555}
5656
5757function endElement ( name ) {
58- if ( currentObject [ '$t' ] ) {
58+ if ( currentObject [ textNodeName ( ) ] ) {
5959 if ( options . trim ) {
60- currentObject [ '$t' ] = currentObject [ '$t' ] . trim ( )
60+ currentObject [ textNodeName ( ) ] = currentObject [ textNodeName ( ) ] . trim ( )
6161 }
6262
6363 if ( options . sanitize ) {
64- currentObject [ '$t' ] = sanitizer . sanitize ( currentObject [ '$t' ] , true ) ;
64+ currentObject [ textNodeName ( ) ] = sanitizer . sanitize ( currentObject [ textNodeName ( ) ] , true ) ;
6565 }
6666
67- currentObject [ '$t' ] = coerce ( currentObject [ '$t' ] , name ) ;
67+ currentObject [ textNodeName ( ) ] = coerce ( currentObject [ textNodeName ( ) ] , name ) ;
6868 }
6969
7070 if ( currentElementName !== name ) {
71- delete currentObject [ '$t' ] ;
71+ delete currentObject [ textNodeName ( ) ] ;
7272 }
7373 // This should check to make sure that the name we're ending
7474 // matches the name we started on.
7575 var ancestor = ancestors . pop ( ) ;
7676 if ( ! options . reversible ) {
77- if ( ( '$t' in currentObject ) && ( Object . keys ( currentObject ) . length == 1 ) ) {
77+ if ( ( textNodeName ( ) in currentObject ) && ( Object . keys ( currentObject ) . length == 1 ) ) {
7878 if ( ancestor [ name ] instanceof Array ) {
79- ancestor [ name ] . push ( ancestor [ name ] . pop ( ) [ '$t' ] ) ;
79+ ancestor [ name ] . push ( ancestor [ name ] . pop ( ) [ textNodeName ( ) ] ) ;
8080 } else {
81- ancestor [ name ] = currentObject [ '$t' ] ;
81+ ancestor [ name ] = currentObject [ textNodeName ( ) ] ;
8282 }
8383 }
8484 }
@@ -112,6 +112,10 @@ function coerce(value,key) {
112112 return value ;
113113}
114114
115+ function textNodeName ( ) {
116+ return options . alternateTextNode ? typeof options . alternateTextNode === 'string' ? options . alternateTextNode : '_t' : '$t'
117+ }
118+
115119
116120/**
117121 * Parses xml to json using node-expat.
@@ -124,6 +128,10 @@ function coerce(value,key) {
124128 * characterized by the presence of the property $t.
125129 * - sanitize_values: If true, the parser escapes any element value in the xml
126130 * that has any of the following characters: <, >, (, ), #, #, &, ", '.
131+ * - alternateTextNode (boolean OR string):
132+ * If false or not specified: default of $t is used
133+ * If true, whenever $t is returned as an end point, is is substituted with _t
134+ * it String, whenever $t is returned as an end point, is is substituted with the String value (care advised)
127135 *
128136 * @return {String|Object } A String or an Object with the JSON representation
129137 * of the XML.
@@ -147,7 +155,8 @@ module.exports = function(xml, _options) {
147155 coerce : joi . alternatives ( [ joi . boolean ( ) , joi . object ( ) ] ) . default ( false ) ,
148156 sanitize : joi . boolean ( ) . default ( true ) ,
149157 trim : joi . boolean ( ) . default ( true ) ,
150- arrayNotation : joi . alternatives ( [ joi . boolean ( ) , joi . array ( ) ] ) . default ( false )
158+ arrayNotation : joi . alternatives ( [ joi . boolean ( ) , joi . array ( ) ] ) . default ( false ) ,
159+ alternateTextNode : [ joi . boolean ( ) . default ( false ) , joi . string ( ) . default ( false ) ]
151160 } ;
152161 var validation = joi . validate ( _options , schema ) ;
153162 hoek . assert ( validation . error === null , validation . error ) ;
@@ -171,6 +180,6 @@ module.exports = function(xml, _options) {
171180
172181 //See: http://timelessrepo.com/json-isnt-a-javascript-subset
173182 json = json . replace ( / \u2028 / g, '\\u2028' ) . replace ( / \u2029 / g, '\\u2029' ) ;
174-
183+
175184 return json ;
176185} ;
0 commit comments