diff --git a/spec/j2x_spec.js b/spec/j2x_spec.js index 828c12cd..af363f92 100644 --- a/spec/j2x_spec.js +++ b/spec/j2x_spec.js @@ -108,6 +108,22 @@ describe("XMLBuilder", function() { expect(result).toEqual(expected); }); + it('should parse to XML with empty CDATA', function() { + const jObj = { + a: { + $cdata: null, + }, + b: { + $cdata: undefined, + }, + }; + const builder = new XMLBuilder({ cdataPropName: '$cdata' }); + const result = builder.build(jObj); + //console.log(result); + const expected = ``; + expect(result).toEqual(expected); + }); + it("should parse to XML with attributes as separate node", function() { const jObj = { a: { diff --git a/src/xmlbuilder/json2xml.js b/src/xmlbuilder/json2xml.js index d35c6921..2274dfaa 100644 --- a/src/xmlbuilder/json2xml.js +++ b/src/xmlbuilder/json2xml.js @@ -92,6 +92,8 @@ Builder.prototype.j2x = function(jObj, level, ajPath) { // null attribute should be ignored by the attribute list, but should not cause the tag closing if (this.isAttribute(key)) { val += ''; + } else if (key === this.options.cdataPropName) { + val += ''; } else if (key[0] === '?') { val += this.indentate(level) + '<' + key + '?' + this.tagEndChar; } else {