The order of attributes printed by this function is undefined, as permitted by the XML spec. You should NOT rely on any implementation detail noted here.
However, in practice, between June 14, 2019 and August 22, 2024, it actually did sort attributes by key name. After August 22, 2024, it changed to track attribute append order and will print them back out in the order in which the keys were first seen.
This is subject to change again at any time. Use toPrettyString if you want a defined output (toPrettyString always sorts by name for consistent diffing).
This is the actual implementation used by toString. You can pass it a preallocated buffer to save some time. Note: the ordering of attributes in the string is undefined. Returns the string it creates.