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));
});
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. For further information, please see how to ask good questions, and take the tour of the site. Commented May 24, 2021 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. npmjs.com/package/xmldom Commented May 24, 2021 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: stackoverflow.com/a/62079497/421195. Commented May 24, 2021 at 23:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.