Skip to content

Configuration to explicitly disable number parsing for specific xml tag values #661

@kule1015

Description

@kule1015
  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?

Description

Hi there,

when parsing an XML tag that can have tag values of "A6" or "6.0", the intention is that the specific XML tag values are always handled as strings. An attempt was made to utilise the parser option "tagValueProcessor". During debugging, it was observed that within the function OrderedObjParser.parseTextData, the function OrderedObjParser.parseValue is called on the result of the callback of tagValueProcessor. The function OrderedObjParser.parseValue will parse the value "6.0" as the integer "6". However, all tag values of "evil" should be parsed as strings.

Did I miss anything or is this intended behavior? Thanks in advance for taking the time!

Input

<evil>6.0</evil>

<evil>A6</evil>

Code

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

const XML_PARSER_OPTIONS = {
  tagValueProcessor: (tagName: string, tagValue: string, jPath: string, hasAttributes: boolean, isLeafNode: boolean) => {
    if (tagName === "evil") {
      return String(tagValue)
    }
    return tagValue;
  },
};

const xmlParser = new XMLParser(XML_PARSER_OPTIONS);

const xml = "<evil>6.0</evil>"

const xml2 = "<evil>A6</evil>"

const parsedXml = xmlParser.parse(xml);
console.log(JSON.stringify(parsedXml))

const parsedXml2 = xmlParser.parse(xml2);
console.log(JSON.stringify(parsedXml2))

Output

{"evil":6}
{"evil":"A6"}

expected data

{"evil":"6.0"}
{"evil":"A6"}

Would you like to work on this issue?

  • Yes
  • No

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions