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 = (xmlNode) => {
const obj = {};
// iterate child elements
const children = xmlNode.*;
for (let i = 0; i < children.length(); i++) {
const child = children[i];
const name = child.name().toString();
if (child.hasSimpleContent()) {
obj[name] = child.toString();
} else {
obj[name] = xmlToObject(child);
}
}
// attributes
const attrs = xmlNode.@*;
for (let i = 0; i < attrs.length(); i++) {
const attr = attrs[i];
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