0

I want to convert:

{
  Employer: {
    Name: 'Sample',
    Age: 23
  }
}

To:

<EMPLOYER NAME="Sample" AGE="23"></EMPLOYER>

Is this possible? Or if you can suggest an npm package I would greatly appreciate it.

Nurio Fernández
  • 518
  • 5
  • 22
Ryan Garde
  • 742
  • 1
  • 7
  • 20
  • That's a little bit tricky... how will that converter choose between creating new nodes or setting data as params? I don't think there is something generic for you. – Nurio Fernández May 06 '21 at 08:30
  • Yes I am having a really hard time too. The big problem is it comes from a third party api so I have no other choice but to work with it. I think its a very old XML format. – Ryan Garde May 06 '21 at 08:36
  • I would google it a little bit before doing something by myself, but if there is no other way, I would use regexp and create a custom serializer/converter for that case. – Nurio Fernández May 06 '21 at 08:38
  • https://stackoverflow.com/questions/48788722/json-to-xml-using-javascript Hope this will help to u – Sachith Wickramaarachchi May 06 '21 at 08:40

1 Answers1

2

Find XML nodes <element><\element>

You can use this regular expression to match the XML nodes

const regex = /['"]?(\w+)['"]?[ ]?:[ ]?{(.[^}]+)}/gms;

Regexp101 Debug link: https://regex101.com/r/71cphG/3

const findNodes = (source) => {
    const regexNodes = /['"]?(\w+)['"]?[ ]?:[ ]?{(.[^}]+)}/gms;

    let nodes = [];
    while (m = regexNodes.exec(str)) {
        let nodeName = m[1]
        let nodeBody = m[2]

        nodes.push({ name: nodeName, body: nodeBody });
    }
    return nodes;
}

Find XML node params age=20

You can match node params with a regular expression like this:

const regex = /\s+(\w+) ?: ?['"]?(.[^'^"\n]+)['"]?/gms;

Regexp101 Debug link: https://regex101.com/r/upMtcz/1

const generateNode = (nodeName, jsonBody) => {
    const regexParams = /\s+(\w+) ?: ?['"]?(.[^'^"\n]+)['"]?/gms;

    let result = `<${nodeName}`;
    while (matches = regexParams.exec(jsonBody)) {
        let param = `${matches[1]}=${matches[2]}`;
        result += ` ${param}`;
    }
    result += `></${nodeName}>`

    return result;
}

Print results

const findNodes = (source) => {
    const regexNodes = /['"]?(\w+)['"]?[ ]?:[ ]?{(.[^}]+)}/gms;

    let nodes = [];
    while (m = regexNodes.exec(str)) {
        let nodeName = m[1]
        let nodeBody = m[2]

        nodes.push({ name: nodeName, body: nodeBody });
    }
    return nodes;
}

const generateNode = (nodeName, jsonBody) => {
    const regexParams = /\s+(\w+) ?: ?['"]?(.[^'^"\n]+)['"]?/gms;

    let result = `<${nodeName}`;
    while (matches = regexParams.exec(jsonBody)) {
        let param = `${matches[1]}=${matches[2]}`;
        result += ` ${param}`;
    }
    result += `></${nodeName}>`

    return result;
}

const str = `{
    "Employer" : {
      Name: 'Sample',
      Age: 23
    },
  Employer: {
      Name: 'Sample',
      Age: 23
    },
  Employer: {
      Name: 'Sample',
      Age: 23
    }
  }`;

findNodes(str)
  .map(({ name, body }) => generateNode(name, body))
  .forEach(element => console.log(element))
Nurio Fernández
  • 518
  • 5
  • 22