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.53k stars 303 forks source link

Data types are set prior to implementing tagValueProcessor #602

Open kmcconnell opened 1 year ago

kmcconnell commented 1 year ago

The following is from another issue that is closed, however it doesn't solve the issue of preserving tag values. It seems data types are set prior to when it tagValueProcessor is implemented which applies transformations to the original data, which is unexpected behavior of a parser.

let options =  {
    parseTagValue: false,
}
let parser = new XMLParser(options)
let object = parser.parse(`
    <?xml version="1.0"?>
    <any_name>
        <person>
            <phone>600.00</phone>
        </person>
    </any_name>
`)
console.info(object)
// { '?xml': '', any_name: { person: { phone: '600.00' } } }

You can also specify a custom tag parser inside the options object (tagValueProcessor property) to handle different cases based on your needs:

let options =  {
    parseTagValue: false,
    tagValueProcessor: (tagName, tagValue) => {
        if (typeof tagValue === 'string') {
            return "HELLO!";
        } else if (tagValue === undefined || tagValue === null) {
            return null;
        }
        return tagValue.toString();
    }
}
let parser = new XMLParser(options)
let object = parser.parse(`
    <?xml version="1.0"?>
    <any_name>
        <person>
            <phone>600.00</phone>
        </person>
    </any_name>
  `)
console.info(object)
// { '?xml': '', any_name: { person: { phone: "HELLO!" } } }

Originally posted by @cecia234 in https://github.com/NaturalIntelligence/fast-xml-parser/issues/597#issuecomment-1642298369

I am using the following parser options:

const builder = new XMLBuilder({
    "ignoreAttributes": false,
    "attributeNamePrefix": "@",
    "textNodeName": "#text",
    "suppressEmptyNode": true,
    "trimValues": false,
    "parseTagValue": false
});

let shipNotice = parser.parse(req.body);

The output results in types set for numbers and booleans even when parseTagValue is false. The desireed effect is that all tag values are preserved as strings without any transformation. Otherwise the integrity of the data is broken.

{
    "?xml": "",
    "ShipNotice": {
        "OrderNumber": "7659061-112-xxxxxxx-xxxxxxx",
        "OrderID": 7659061, // should be string
        "CustomerCode": "np9n2kb9876f3xw@xxxxxxx.com",
        "CustomerNotes": "",
        "InternalNotes": "",
        "NotesToCustomer": "",
        "NotifyCustomer": true,
        "LabelCreateDate": "08/09/2023 19:53",
        "ShipDate": "08/09/2023",
        "Carrier": "DHLGlobalMail",
        "Service": "DHL SmartMail Parcel Ground",
        "TrackingNumber": 4.20xxxx74811e+29, // from long string of digits, should be left string
        "ShippingCost": 0, // from 0.00
        "CustomField1": "",
        "CustomField2": "",
        "CustomField3": "",
        "Recipient": {
            "Name": "xxxxxxx",
            "Company": "",
            "Address1": "xxxxxxx",
            "Address2": "",
            "City": "xxxxxxx",
            "State": "xx",
            "PostalCode": "45807-9581",
            "Country": "xx"
        },
        "Items": {
            "Item": {
                "SKU": "xxxxxxx",
                "Name": "xxxxxxx",
                "Quantity": 2, // should be string
                "LineItemID": 9281347 // should be string
            }
        }
    }
}
github-actions[bot] commented 1 year ago

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.