0

Using npm package xml-js I tried to convert some xml string with utf-8 characters in it. The method xml2js(..) is used. It scrmabled utf-8 characters like german umlaut chracters. Please take a look at the bellow example and consequent result:

var convert = require('xml-js');
var xml =
'<?xml version="1.0" encoding="utf-8"?>' +
'<note importance="high" logged="true">' +
'    <title>Deutsche TODOs</title>' +
'    <todo>Ihr seid schön!</todo>' +
'    <todo>Schönen abend sagen zur meine Frau!</todo>' +
'</note>';
var result1 = convert.xml2json(xml, {compact: true, spaces: 4});
console.log(result1, '\n', result1);

Result1 It simply scrambles the text with umlauts inside the <todo> tags:

 {
    "_declaration": {
        "_attributes": {
            "version": "1.0",
            "encoding": "utf-8"
        }
    },
    "note": {
        "_attributes": {
            "importance": "high",
            "logged": "true"
        },
        "title": {
            "_text": "Deutsche TODOs"
        },
        "todo": [
            {
                "_text": "Ihr seid schön!"
            },
            {
                "_text": "Schönen abend sagen zur meine Frau!"
            }
        ]
    }
}

EDIT: The original text that I was running into trouble with has been given in the link below in the runkit link:

https://runkit.com/cfmes/5d1cb38d0d5e21001b5d4677

The problem is it's not reproduceable in the runkit app but in my app on the powershell console, the scrambledtext is showing all the time even though I have updated from version xml-js@1.6.8 to xml-js@1.6.11.

Got any solution for this?

Regards Emdadul

edam
  • 910
  • 10
  • 29
  • The umlauts print OK here using `xml-js@1.6.11` running on Win and/or macOS. Which version of `xml-js` and which OS are you using? – RobC Jul 03 '19 at 12:31
  • I'm able to reproduce your issue if I save your node.js script from something like Windows Notepad application using ANSI encoding, i.e when the `.js` file/script is encoded using charset `iso-8859-1` instead of the desired charset `utf-8`. – RobC Jul 03 '19 at 13:14
  • @RobC I'm using `xml-js@1.6.8` but using `xml-js@1.6.11` also emits the same scrambled result for me. The OS is Windows 10. In my real scenario I'm taking some text excerpt from a file. tHe file is as well opened using `const xml = require('fs').readFileSync(xmlFile, 'utf8');` the excerpt is taken from the file and fed into `xml2js(..)` method. I will edit my question and put the real text to provide the real scenario. – edam Jul 03 '19 at 13:48

0 Answers0