Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data types are set prior to implementing tagValueProcessor #602

Open
kmcconnell opened this issue Aug 16, 2023 · 1 comment
Open

Data types are set prior to implementing tagValueProcessor #602

kmcconnell opened this issue Aug 16, 2023 · 1 comment

Comments

@kmcconnell
Copy link

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": "[email protected]",
        "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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant