NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.
https://naturalintelligence.github.io/fast-xml-parser/
MIT License
2.49k stars 302 forks source link

Incorrect parsing entity < #501

Closed AllanZhengYP closed 1 year ago

AllanZhengYP commented 1 year ago

Description

The predefined entity seems to be parsed twice. &amp;lt; is expected to be parsed to $lt;. However, it is parsed again to <

Input

<SimpleScalarPropertiesInputOutput>
  <stringValue>&amp;lt;</stringValue>
</SimpleScalarPropertiesInputOutput>

Code

import { XMLParser } from "fast-xml-parser";

const encoded = `<SimpleScalarPropertiesInputOutput>
  <stringValue>&amp;lt;</stringValue>
</SimpleScalarPropertiesInputOutput>`;
const parser = new XMLParser({})
const parsed = parser.parse(encoded);

console.log(JSON.stringify(parsed, null, 2));

Output

{
  "SimpleScalarPropertiesInputOutput": {
    "stringValue": "<"
  }
}

expected data

{
  "SimpleScalarPropertiesInputOutput": {
    "stringValue": "&lt;"
  }
}

Would you like to work on this issue?

It seems to be caused by replacing the escaped value with entity regex in a loop: https://github.com/NaturalIntelligence/fast-xml-parser/blob/443e8edaf94bb1e65738cd73aa32c9d5fa06a927/src/xmlparser/OrderedObjParser.js#L372-L375

AllanZhengYP commented 1 year ago

Hi @amitguptagwl, is there any update?