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.

4
  • 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. Commented May 6, 2021 at 8: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. Commented May 6, 2021 at 8: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. Commented May 6, 2021 at 8:38
  • stackoverflow.com/questions/48788722/… Hope this will help to u Commented May 6, 2021 at 8:40

1 Answer 1

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))

Sign up to request clarification or add additional context in comments.

1 Comment

I have added a few code examples for you, as I said before, I would google for a better approach if there is any.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.