Understand Compact vs Non-Compact modes
masterxml-js supports two modes for representing XML in JSON/JavaScript objects:
Compact Mode ({compact: true})
- Pros: Concise and saves space.
- Cons: Can lose the original order of elements if elements with the same name are mixed with other elements. For example,
<a/><b/><a/>might be merged into{a:[{},{}], b:{}}, losing the sequencea -> b -> a. - Use case: When space is a priority and element order/reversibility is not critical.
Non-Compact Mode ({compact: false})
- Pros: Guarantees the preservation of element order and is fully reversible. It uses an
elementsarray to store nodes, ensuring that even if multiple nodes have the same name, their sequence is maintained. - Cons: More verbose and consumes more memory/space.
- Use case: When you need to convert the JSON back to the exact original XML or when element order matters.
Tip: You can reduce the output size of non-compact mode by using shorter key names via configuration options.