1

i just know that the object Json can be an XMl file by the js2xml library, so that's why I'm trying to convert the following json to XML, How can I achieve this in NodeJS? i can't find an answer or a documentation that can help me?

here is the model JSON

const UserSchema = new mongoose.Schema({

email: {
type: String,
required: [true, "Please provide email address"],
unique: true,
match: [
  /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9] 
{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
  "Please provide a valid email",
],
},
password: {
type: String,
required: [true, "Please add a password"],
minlength: 6,
select: false,
 },
 

const User = mongoose.model("User", UserSchema);

module.exports = User;

i used this exemple that didn't work for me

  function groupChildren(obj) {
  for(prop in obj) {
  if (typeof obj[prop] === 'object') {
  groupChildren(obj[prop]);
   } else {
  obj['$'] = obj['$'] || {};
  obj['$'][prop] = obj[prop];
  delete obj[prop];
  }
  }

 return obj;
 }

 const xml2js = require('xml2js'); 
 const obj = {
 Level1: {
 attribute: 'value',
 Level2: {
  attribute1: '05/29/2020',
  attribute2: '10',
  attribute3: 'Pizza'
  }
  }
   };

 
   const builder = new xml2js.Builder();
  const xml = builder.buildObject(groupChildren(obj));
     console.log(xml);
Safinez Hl
  • 21
  • 3

2 Answers2

0

When converting JSON to XML, one has to ask: what XML do you want to convert it to? Does it have to be a specific XML format, or will any old XML do?

If any old XML will do, then you can usually find some library to do the job, such as js2xml or js2xmlparser. The problem with these libraries is that they usually offer very little control over how the XML is generated, especially for example if there are JSON objects with keys that are not valid XML names (which doesn't apply in your case).

If you want a specific XML format then I would recommend using XSLT 3.0, which is available on node.js in the form of Saxon-JS. [Disclaimer: my company's product]. If you are interested in pursuing this approach, then tell us what you want the output to look like, and we can help you create it.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • i just want a simple xml ,i did use the library js2xml but it dosen't work – Safinez Hl May 25 '21 at 21:41
  • If you want help to get something working, it's best to say exactly what you did, exactly what happened, and how that differs from what you expected. Telling us it "didn't work" is like telling your doctor you don't feel well; they're going to want more information. – Michael Kay May 26 '21 at 17:33
0

There are many different packages for XML serialization.

  • Most of them enforce a specific XML and JSON mapping convention.
  • Others require you to build the XML document in code.

Finally, there are solutions that do this with decorators. Those give you freedom in defining the structure without having to build the document entirely in code.

As an example: the xml decorators package.

It means that you define the XML mapping using a class. Next, you define decorators on top of each field, to define how it should be mapped to XML.

import { XMLAttribute, xml } from 'xml-decorators';
 
const NS = 'ns';

export class User {
  @XMLAttribute({namespace: NS})
  private email:string;
 
  @XMLAttribute({namespace: NS})
  private password: string;

  constructor(email: string, password: string) {
    this.email = email;
    this.password = password;
  }
}

And finally, to actually serialize

const user = new User('foo@bar.com', 'secret');
const xml = xml.serialize(user);

Conceptually, this is certainly a robust solution, since it strongly resembles how java xml binding (JAXB) and C# xml serialization work.

bvdb
  • 22,839
  • 10
  • 110
  • 123