node-xml2js
node-xml2js copied to clipboard
Add forceCdata option to builder to force the use of CDATA tags for all content.
This PR adds a forceCdata
option to the XML builder that wraps all text nodes in CDATA even when it is not required. This option is set to false
by default. A new test exercises the forceCdata
option using a small sample of text that does not require escaping.
Coverage remained the same at 97.633% when pulling 16ce472b58dcdbceacc66413a2bacd9254d132d9 on mdaly:force-cdata-builder-option into 434abf4ca99fc780ae2f905a400449889dd88520 on Leonidas-from-XIV:master.
Why would I want to wrap things in unnecessary CDATA
?
We've run into a situation where a parser (that we don't control) requires CDATA on all text content, like what's described in #321. I'm not sure how common this is, but the ability to always force CDATA has served us well.
Truth be told, I am not very fond of outputting nonsensical XML just because something that claims it parses XML does not do it properly. It is like saying "we have a parser that doesn't handle characters, so we should encode every character via XML entities".
@Leonidas-from-XIV We met the same problem, some company require CDATA on all text content, but we can't say they're wrong and fix for us. So we have to change our code in workspace use CDATA force. We hope there'll be coming a long-term solution.
same problem here
Same for us. One of the german services requires all the data to be wrapped into CDATA
In my case, I have a bunch of attributes containing special characters like "&", it gets converted to to "$amp;"...is there any way I can avoid this add make all my attributes retain values using CDATA?
Same problem here
+1 The Tecent wechat api require CDATA on all content.
Same problem with Wechat. Now I have to use a template string instead of xml2js
to force CDATA. Painful as it might, I still consider a forceCdata
option quite useful.
Anything?
Same problem with Wechat. Now I have to use a template string instead of
xml2js
to force CDATA. Painful as it might, I still consider aforceCdata
option quite useful.
Just wanted to elaborate on this suggestion. It could be implemented as the following:
- Walk through all object fields that need to be in CDATA tag and append to their values a string that will force CDATA generation e.g.
value += '<FORCECDATA>'
. - Build XML.
- Remove all occurrences of the appended string from the XML string, e.g.
xml = xml.ReplaceAll('<FORCECDATA>', '')
.