https://github.com/daniel-sc/xml-trueformat
Typescript XML parser that 100% retains all formatting for creating identical XML on roundtrips
https://github.com/daniel-sc/xml-trueformat
typescript xml xml-parser
Last synced: 12 months ago
JSON representation
Typescript XML parser that 100% retains all formatting for creating identical XML on roundtrips
- Host: GitHub
- URL: https://github.com/daniel-sc/xml-trueformat
- Owner: daniel-sc
- License: mit
- Created: 2025-02-24T12:16:07.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-16T12:04:05.000Z (about 1 year ago)
- Last Synced: 2025-06-19T22:14:28.215Z (about 1 year ago)
- Topics: typescript, xml, xml-parser
- Language: TypeScript
- Homepage:
- Size: 114 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/xml-trueformat)
[](https://coveralls.io/github/daniel-sc/xml-trueformat?branch=main)
# xml-trueformat
Typescript XML parser that 100% retains all formatting for creating identical XML on roundtrips.
If you have an XML file that you want to read, modify, and write back, this library will ensure that the output XML is formatted identical to the input XML.
Check out the following [blog post](https://dev.to/danielsc/deep-dive-xml-trueformat-preserve-xml-formatting-with-ease-46f5) for more details.
## Features
- Retains all whitespace and line endings
- Retains all comments
- Retains whether an element is self-closing or not
- Retains attributes in the order they were defined
- Retains attribute quotes (single or double) and whitespace before and after the attribute name
- Retains XML processing instructions (including the XML declaration)
- Retains CDATA sections
- Parses Angular templates and other HTML/XML-like files
## Example
The following XML:
```xml
]]>
```
Will get parsed by `XmlParser.parse(..)` to:
```ts
new XmlDocument([
new XmlProcessing('xml', ' ', 'version="1.0" encoding="UTF-8"'),
new XmlText('\n'),
new XmlElement('root', [], [
new XmlText('\n '),
new XmlComment(' my comment '),
new XmlText('\n '),
new XmlElement('self-closing', [new XmlAttribute('attribute', 'value with double quotes')], [], ' ', true),
new XmlText('\n '),
new XmlElement('non-self-closing', [new XmlAttribute('attribute', 'value with single quotes', ' ', '', '', "'")], [], '', false),
new XmlText('\n '),
new XmlElement('element-with-cdata', [], [new XmlCData('')]),
new XmlText('\n')
])
]);
```
And serializing this again with `XmlDocument.toString()` will exactly give the original XML!
## Development
This project is written in Typescript and uses Bun.
```bash
bun install
bun test
```