Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.88k stars 602 forks source link

Builder with CDATA doesn't work ! #535

Open brunovu20 opened 4 years ago

brunovu20 commented 4 years ago

Hi guys,

I have a problem on the builder with CDATA. Here is just a simple example on ReadMe:

`var xml2js = require('xml2js');

var obj = {root: {$: {id: "my id"}, _: "my inner text"}};

var builder = new xml2js.Builder({ cdata: true }); var xml = builder.buildObject(obj);`

Xml generated:

`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

my inner text` **So it misses CDATA wrapping "my inner text"** I see the code of node-xml2js, because of this line: https://github.com/Leonidas-from-XIV/node-xml2js/blob/aefc64af9e9badbd0aade00814548f2ea33c9b4e/src/builder.coffee#L7 `requiresCDATA = (entry) -> return typeof entry is "string" && (entry.indexOf('&') >= 0 || entry.indexOf('>') >= 0 || entry.indexOf('<') >= 0)` I think we don't need that. Because it tested **_ and option cdata** of builder Can you vote this to remove this condition. Thank you
TanninOne commented 4 years ago

Yes please! I have to generate xml for a foreign application and they will throw an error if certain fields aren't wrapped in CDATA. So either remove the condition or have the cdata flag as a tristate option like true | false | 'force' where force overrides the condition, that way you stay backwards compatible.

brunovu20 commented 4 years ago

I fixed that on this repository: https://github.com/brunovuftv2018/node-xml2js

Samicelus commented 4 years ago

Yes please! I have to generate xml for a foreign application and they will throw an error if certain fields aren't wrapped in CDATA. So either remove the condition or have the cdata flag as a tristate option like true | false | 'force' where force overrides the condition, that way you stay backwards compatible.

yes, I met the same problem. Please remove this condition or perhaps add anothor option named forceCdata?