Attempting to convert an XSD to JSON - javascript

I am attempting to convert an XSD to JSON using the XSD2JSON2 package. I have tried the below, but the users.json file is not created
var xsd2json = require('xsd2json2').xsd2json;
var fs = require('fs');
xsd2json('users.xsd', (err, jsonSchema) => {
fs.writeFile('users.json', jsonSchema, (err) => {
if (err) throw err;
console.log('JSON schema created')
});
});
I tried the above after getting a 'callback not defined' issue with XSD2JSON2's usage code:
var xsd2json = require('xsd2json2').xsd2json;
var fs = require('fs');
xsd2json('./users.xsd', function(err, jsonSchema) {
fs.writeFile('./users.json', jsonSchema, callback);
});
I am currently learning JS (as you can probably tell) so any assistance would be appreciated.

Related

Cannot read and parse XML file in Meteor

Im using xml2jws and Meteor. Ive tried both with fs and without like the examples in docs
my js file With fs
var xml2js = require('xml2js');
var fs = require('fs');
var parser = new xml2js.Parser();
export const extractEntities = function (file) {
fs.readFile('./xmlFile.xml', function(err, data) {
parser.parseString(data, function (err, result) {
console.dir(result);
console.log('Done');
});
});
Error
Uncaught TypeError: fs.readFile is not a function
at extractEntities (packages.js:8)
at PackageUpload.componentDidMount (PackageUpload.js:36)
at commitLifeCycles (modules.js?hash=603e8fa158a1fc409b1d0d581586111b31091ac9:24142)
js file Without fs
const parseString = require('xml2js').parseString;
export const extractEntities = () => {
parseString('./xmlFile.xml', function (err, result) {
console.dir(result);
return result;
})
}
In this case, it returns undefined
However, when I pass an xml string in parseStringit works. But in my app, Im uploading it as a file from disk and its passed to extractEntities. It has no arguments in this example because im testing parsing the file first, then will test an uploaded xml file.
This is my directory
Directory
file.js
xmlFile.xml

Node Winston log file forced string conversion

In a Node project, I want to show the contents of a Winston log file in a React interface. Reading the file:
let content;
fs.readFile("path", "utf-8", function read(err, data) {
if (err)
throw err;
content = data;
});
I send them to the interface:
router.get("/", function (req, res) {
res.status(200).send(JSON.stringify(content));
});
And i get the content in a .jsx file:
getLogs().then(res => {
let datafromfile = JSON.parse(res);
// Use the data
return;
}).catch(err => {
return err.response;
});
The issue i am having is that fs converts all the data into a string (since i am putting the utf-8 encoding and do not want to be returned a buffer) and therefore i cannot manipulate the objects in the log file to show them structurally in the interface. Can anyone guide how to approach this problem?
I have not debugged this, but a lot of this depends on whether or not the the Winston file your loading actually has JSON in it.
If it does then JSONStream is your friend and leaning through or through2 is helpful you in node (streams).
following, code/pseudo
router.get("/", function (req, res) {
const logPath = ‘somePath’; // maybe it comes from the req.query.path
const parsePath = null; // or the token of where you want to attemp to start parsing
fs.createReadStream(logPath)
.pipe(JSONStream.parse(parsePath))
.pipe(res);
});
JSONStream
fs.createReadStream and node docs
through2

How to kill a Python-Shell module process in Node.js?

I am currently using the python-shell module in a Node based web interface. The issue I am having is mostly syntactical. The code below shows the generation of a python script.
var PythonShell = require('python-shell');
PythonShell.run('my_script.py' function (err) {
if (err) throw err;
console.log('finished');
}):
This is just an example script from here. How do I relate this to node's
var procc = require('child_process'.spawn('mongod');
procc.kill('SIGINT');
The documentation states that PythonShell instances have the following properties:
childProcess: the process instance created via child_process.spawn
But how do I acutally use this? There seems to be a lack of examples when it comes to this specific module
For example -
var python_process;
router.get('/start_python', function(req, res) {
const {PythonShell} = require("python-shell");
var options = {
pythonPath:'local python path'
}
var pyshell = new PythonShell('general.py');
pyshell.end(function (err) {
if (err) {
console.log(err);
}
});
python_process = pyshell.childProcess;
res.send('Started.');
});
router.get('/stop_python', function(req, res) {
python_process.kill('SIGINT');
res.send('Stopped');
});

How to check file content using nodejs?

I want to see content of the file that is posted from the client i am using fs module so with below code contents is coming undefined , Any idea what is missing in below code ?
I have file printed in server side to make sure i am gettign the data.
server.js
var data = new multiparty.Form();
var fs = require('fs');
export function create(req, res) {
data.parse(req, function(err,files) {
var file = files.file;
console.log(file);
fs.readFile(file, 'utf8', function(err, contents) {
console.log('content',contents);
});
});
};
I guess the problem might be the signature of the callback you are supplying to data.parse (you are missing the fields argument).
Check it yourself by looking to the examples on multiparty docs
var data = new multiparty.Form();
var fs = require('fs');
export function create(req, res) {
data.parse(req, function(err, fields, files) {
var file = files.file;
console.log(file);
fs.readFile(file, 'utf8', function(err, contents) {
console.log('content',contents);
});
});
};

fs.readFileSync Error

I am trying to use the html-pdf node module to generate pdfs from html. I am currently running this using the cloud9 IDE.
My code is:
var fs = require("fs");
var pdf = require('html-pdf');
var html = fs.readFileSync('./test.html', {encoding: 'utf8'});
var options = { format: 'Letter' };
app.post('/pdf',function(req, res) {
pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
if (err) return console.log(err);
console.log(res); // { filename: '/app/businesscard.pdf' }
});
});
I get the following Error:
[Error: Fontconfig warning: ignoring C.UTF-8: not a valid language tag]
Does anyone know how i can resolve this issue?
This is due to a bug in fontconfig. You can see here
Open your terminal and execute locale -a you will see list of fonts. Then select it like LC_ALL=C
may it can help

Categories