Skip to content

Instantly share code, notes, and snippets.

@dzmitry-savitski
Last active March 4, 2026 13:19
Show Gist options
  • Select an option

  • Save dzmitry-savitski/5658699aa1ae60d028afda0b709ea32b to your computer and use it in GitHub Desktop.

Select an option

Save dzmitry-savitski/5658699aa1ae60d028afda0b709ea32b to your computer and use it in GitHub Desktop.
XML to javascript
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