xml-js
xml-js copied to clipboard
json to xml: Auto convert < to <
I'm using json2xml
var convert = require('xml-js');
var json = require('fs').readFileSync('test.json', 'utf8');
var options = {compact: true, ignoreComment: true, spaces: 4};
var result = convert.json2xml(json, options);
console.log(result);
Here is test.json file
{
"value": [
{
"_text": "\"test < \""
}
]
}
Result:
<value>"test < "</value>
Expect:
<value>"test < "</value>
Is there any way to ignore auto convert < to <
@anhuet
The sanitize options can ignore this behavior, but it has been deprecated.
The follow code can help you:
const res = convert.json2xml(
JSON.stringify({
value: [
{
_text: '"test < "',
},
],
}),
{
compact: true,
textFn: (value) => {
return value.replace("<", "<");
},
}
);
// <value>"test < "</value>
console.log(res);