node-xml2js icon indicating copy to clipboard operation
node-xml2js copied to clipboard

Add forceCdata option to builder to force the use of CDATA tags for all content.

Open mdaly opened this issue 7 years ago • 13 comments

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.

mdaly avatar Aug 08 '17 20:08 mdaly

Coverage Status

Coverage remained the same at 97.633% when pulling 16ce472b58dcdbceacc66413a2bacd9254d132d9 on mdaly:force-cdata-builder-option into 434abf4ca99fc780ae2f905a400449889dd88520 on Leonidas-from-XIV:master.

coveralls avatar Aug 08 '17 20:08 coveralls

Why would I want to wrap things in unnecessary CDATA?

Leonidas-from-XIV avatar Aug 08 '17 21:08 Leonidas-from-XIV

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.

mdaly avatar Aug 08 '17 22:08 mdaly

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 avatar Aug 11 '17 13:08 Leonidas-from-XIV

@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.

superl2080 avatar Dec 31 '17 18:12 superl2080

same problem here

sirius1024 avatar Jan 12 '18 07:01 sirius1024

Same for us. One of the german services requires all the data to be wrapped into CDATA

kirpichenko avatar Feb 20 '18 17:02 kirpichenko

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?

noumantahir avatar Mar 27 '18 12:03 noumantahir

Same problem here

ryanthegiantlion avatar Jun 27 '18 15:06 ryanthegiantlion

+1 The Tecent wechat api require CDATA on all content.

kennylbj avatar Feb 12 '19 02:02 kennylbj

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.

std4453 avatar May 17 '19 09:05 std4453

Anything?

tal-rofe avatar Jul 02 '22 12:07 tal-rofe

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.

Just wanted to elaborate on this suggestion. It could be implemented as the following:

  1. 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>'.
  2. Build XML.
  3. Remove all occurrences of the appended string from the XML string, e.g. xml = xml.ReplaceAll('<FORCECDATA>', '').

dmitryshk avatar Oct 03 '22 02:10 dmitryshk