oozcitak / xmlbuilder-js

An XML builder for node.js
MIT License
919 stars 108 forks source link

Support encodings #258

Open LucCappellaro opened 1 year ago

LucCappellaro commented 1 year ago

The Builder-Options allow to specify a custom encoding. However this has no effect, except changing the xml declaration. According to the XML spec:

... it is a fatal error for an entity including an encoding declaration to be presented to the XML processor in an encoding other than that named in the declaration, ...

Browsers and NodeJS support multiple encodings. It would be great if xmlbuilder-js would support this too.

To be clear, changing the encoding can have an impact on the XML representation. Unsupported character need to be escaped with numeric character references.

Example in UTF-8:

<?xml version="1.0" encoding="UTF-8"?>
<test>
  <english>Hello World</english>
  <portuguese>Olá Mundo</portuguese>
  <czech>Ahoj světe</czech>
  <chinese>你好,世界</chinese>
</test>

Same example in ISO-8859-1:

<?xml version="1.0" encoding="ISO-8859-1"?>
<test>
  <english>Hello World</english>
  <portuguese>Olá Mundo</portuguese>
  <czech>Ahoj sv&#283;te</czech>
  <chinese>&#20320;&#22909;&#65292;&#19990;&#30028;</chinese>
</test>