Last active
March 4, 2026 13:19
-
-
Save dzmitry-savitski/5658699aa1ae60d028afda0b709ea32b to your computer and use it in GitHub Desktop.
XML to javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const xmlToObject = (xml) => { | |
| const obj = {}; | |
| // children | |
| Array.from(xml.*).forEach(child => { | |
| const name = child.name().toString(); | |
| obj[name] = child.hasSimpleContent() | |
| ? child.toString() | |
| : xmlToObject(child); | |
| }); | |
| // attributes | |
| Array.from(xml.@*).forEach(attr => { | |
| obj[`@${attr.name()}`] = attr.toString(); | |
| }); | |
| return obj; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment