Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json to xml: Auto convert < to &lt #209

Open
anhuet opened this issue Oct 3, 2023 · 1 comment
Open

json to xml: Auto convert < to &lt #209

anhuet opened this issue Oct 3, 2023 · 1 comment

Comments

@anhuet
Copy link

anhuet commented Oct 3, 2023

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 &lt; "</value>

Expect:
<value>"test < "</value>

Is there any way to ignore auto convert < to &lt

@liuyib
Copy link

liuyib commented Jun 4, 2024

@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("&lt;", "<");
      },
    }
  );

  // <value>"test < "</value>
  console.log(res);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants