Skip to content

Data types are set prior to implementing tagValueProcessor #602

@kmcconnell

Description

@kmcconnell

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 #597 (comment)

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
            }
        }
    }
}

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