1

I can't find documentation that can help me or some examples. I created a model (json object) for my data base (mongodb) then I created a form in the client side (react) to generate my data. I want to know how can I take this data and convert it into an XML file. All I know is JSon object can be converted in xml file I tried some things but they don't work.

This my model:

const mongoose = require('mongoose');

const fileSchema = mongoose.Schema({
  title: {
    type: String,
    trim: true
  },
  file_path: {
    type: String,
    required: true
  },
  file_mimetype: {
    type: String,
    required: true
  }
}, {
  timestamps: true
});

const File = mongoose.model('File', fileSchema);

module.exports = File;

and this is my server.js

require("dotenv").config({ path: "./config.env" });
const express = require("express");
const app = express();
const errorHandler = require("./middleware/error");
const path = require('path');
const fileRoute = require('./routes/file');
const Cors = require('Cors');

const mongoose = require('mongoose')
require('dotenv').config();

app.use(Cors());

// Connect DB
const PORT = process.env.PORT || 5000;
const url = process.env.MONGO_URL;
mongoose.connect(url, {
  useNewUrlParser: true, 
  useUnifiedTopology: true, 
  useCreateIndex: true, 
  useFindAndModify: false,
})
.then(() => {
  console.log("Server up and running!")
})

app.use(express.json());
app.get("/", (req, res, next) => {
  res.send("Api running");
});

// Connecting Routes
app.use("/api/auth", require("./routes/auth"));
app.use("/api/private", require("./routes/private"));
//aploadfile

app.use(express.static(path.join(__dirname, '..', 'build')));
app.use(fileRoute);

app.get("/upload", (req, res) => {
  res.sendFile(path.join(__dirname, '..', 'build', 'index.html'));
});

// Error Handler Middleware
app.use(errorHandler);

const server = app.listen(PORT, () =>
  console.log(`Sever running on port ${PORT}`)
);

process.on("unhandledRejection", (err, promise) => {
  console.log(`Logged Error: ${err.message}`);
  server.close(() => process.exit(1));
});
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
Safinez Hl
  • 21
  • 3
  • 1
    Welcome to StackOverflow! Can you share what you've tried so far? StackOverflow isn't a free code-writing service, and expects you to try to solve your own problem first. Please update your question to show what you have already tried, showing the specific problem you are facing in a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). For further information, please see [how to ask good questions](http://stackoverflow.com/help/how-to-ask), and take the [tour of the site](http://stackoverflow.com/tour). – coagmano May 24 '21 at 22:28
  • 1
    There is no direct conversion. They're different file formats. You will have to decide how you want to structure your data, based on your requirements. You should probably consider some package that provides DOMDocument to build your XML document programmatically, and then output the XML. https://www.npmjs.com/package/xmldom – Brad May 24 '21 at 22:29
  • Like Brad said, JSON and XML are two completely different formats. Your best bet is generally 1) parse the input JSON, 2) manually construct the output XML. *HOWEVER* ... there are 3rd party libraries which might be able to do some (or all!) of the work for you. For example, [xml-js](https://www.npmjs.com/package/xml-js): https://stackoverflow.com/a/62079497/421195. – paulsm4 May 24 '21 at 23:02

0 Answers0