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

01 is parsed as number, but string is wanted #513

Closed modestotech closed 1 year ago

modestotech commented 1 year ago

Description

I want to have a xml value 01 to be parsed as a string 01, but it's parsed as number. I've tested the XML below in the tool where it works as intended. I suspect there's something with my parserOptions, but can't see it.

Input

<?xml version="1.0" encoding="UTF-8"?>
<Catalog>
    <Articles>
        <HeaderData>
            <Date />
            <CatalogName />
        </HeaderData>
        <ArticleData>
            <ArticleNumber>1234</ArticleNumber>
            <Artikkelkategori>
                <ID>01</ID>
                <Value>Samleartikkel</Value>
            </Artikkelkategori>
        </ArticleData>
    </Articles>
</Catalog>

Code

        const alwaysArray = ['Catalog.Articles.ArticleData.OtherEAN'];

        const parserOptions = {
            ignoreDeclaration: true,
            isArray: (name: string, jpath: string) => {
                if (alwaysArray.indexOf(jpath) !== -1) {
                    return true;
                }
                return false;
            },
            parseTrueNumberOnly: true,
        };

        const parser = new XMLParser(parserOptions);
        const parsedObject = parser.parse(downloadedFile[0]);

Output

image

expected data

I expect ArtikkelKategori.ID to be string 01 but it's a number.

Would you like to work on this issue?

Bookmark this repository for further updates.

github-actions[bot] commented 1 year ago

I'm glad you find this repository helpful. I'll try to address your issue ASAP. You can watch the repo for new changes or star it.

opunbuds commented 1 year ago

+1

amitguptagwl commented 1 year ago

Please check the documentation. I hope you're using version v of the library

modestotech commented 1 year ago

Yes, Google had taken me to the documentation for v3. Worked fine for v4 with the following:

            numberParseOptions: {
                leadingZeros: false,
                hex: false,
            }