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.

Revisions

  1. dzmitry-savitski revised this gist Mar 4, 2026. No changes.
  2. dzmitry-savitski revised this gist Mar 4, 2026. 1 changed file with 14 additions and 20 deletions.
    34 changes: 14 additions & 20 deletions convert.js
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,19 @@
    const xmlToObject = (xmlNode) => {
    const obj = {};
    const xmlToObject = (xml) => {
    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();
    // children
    Array.from(xml.*).forEach(child => {
    const name = child.name().toString();

    if (child.hasSimpleContent()) {
    obj[name] = child.toString();
    } else {
    obj[name] = xmlToObject(child);
    }
    }
    obj[name] = child.hasSimpleContent()
    ? child.toString()
    : xmlToObject(child);
    });

    // attributes
    const attrs = xmlNode.@*;
    for (let i = 0; i < attrs.length(); i++) {
    const attr = attrs[i];
    obj[`@${attr.name()}`] = attr.toString();
    }
    // attributes
    Array.from(xml.@*).forEach(attr => {
    obj[`@${attr.name()}`] = attr.toString();
    });

    return obj;
    return obj;
    };
  3. dzmitry-savitski revised this gist Mar 4, 2026. 1 changed file with 13 additions and 12 deletions.
    25 changes: 13 additions & 12 deletions convert.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,11 @@
    function xmlToObject(xml) {
    var obj = {};
    const xmlToObject = (xmlNode) => {
    const obj = {};

    for each (var child in xml.*) {
    var name = child.name().toString();
    // 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();
    @@ -11,14 +14,12 @@ function xmlToObject(xml) {
    }
    }

    for each (var attr in xml.@*) {
    obj["@" + attr.name()] = attr.toString();
    // attributes
    const attrs = xmlNode.@*;
    for (let i = 0; i < attrs.length(); i++) {
    const attr = attrs[i];
    obj[`@${attr.name()}`] = attr.toString();
    }

    return obj;
    }

    var xml = new XML(xmlString);
    var result = xmlToObject(xml);

    logger.info(JSON.stringify(result));
    };
  4. dzmitry-savitski created this gist Mar 4, 2026.
    24 changes: 24 additions & 0 deletions convert.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    function xmlToObject(xml) {
    var obj = {};

    for each (var child in xml.*) {
    var name = child.name().toString();

    if (child.hasSimpleContent()) {
    obj[name] = child.toString();
    } else {
    obj[name] = xmlToObject(child);
    }
    }

    for each (var attr in xml.@*) {
    obj["@" + attr.name()] = attr.toString();
    }

    return obj;
    }

    var xml = new XML(xmlString);
    var result = xmlToObject(xml);

    logger.info(JSON.stringify(result));