Generate file with nodejs

Asked

Viewed 484 times

0

I have the following problem, I have an xml to json converter in javascript and this converter saves the converted object in a variable. I need to adapt this code to make it possible to write the file from the nodejs, but so far I have not been able to solve the problem. Converter code:

function XMLtoJSON() {
    var me = this; //
 
    me.fromFile = function(xml, rstr) {
        var xhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
        // sets and sends the request for calling "xml"
        xhttp.open("GET", xml, false);
        xhttp.send(null);

        // gets the JSON string
        var json_str = jsontoStr(setJsonObj(xhttp.responseXML));

        return (typeof(rstr) == 'undefined') ? JSON.parse(json_str) : json_str;
    }

    me.fromStr = function(xml, rstr) {
        if (window.DOMParser) {
            var getxml = new DOMParser();
            var xmlDoc = getxml.parseFromString(xml, "text/xml");
        } else {
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
        }

        var json_str = jsontoStr(setJsonObj(xmlDoc));
        return (typeof(rstr) == 'undefined') ? JSON.parse(json_str) : json_str;
    }

    var setJsonObj = function(xml) {
        var js_obj = {};
        if (xml.nodeType == 1) {
            if (xml.attributes.length > 0) {
                js_obj["@attributes"] = {};
                for (var j = 0; j < xml.attributes.length; j++) {
                    var attribute = xml.attributes.item(j);
                    js_obj["@attributes"][attribute.nodeName] = attribute.value;

                }
            }
        } else if (xml.nodeType == 3) {
            js_obj = xml.nodeValue;
        }
        if (xml.hasChildNodes()) {
            for (var i = 0; i < xml.childNodes.length; i++) {
                var item = xml.childNodes.item(i);
                var nodeName = item.nodeName;
                if (typeof(js_obj[nodeName]) == "undefined") {
                    js_obj[nodeName] = setJsonObj(item);
                    if (nodeName == "child") {
                        //js_obj[nodeName] = setJsonObj(item);
                        js_obj[nodeName] = [];
                        js_obj[nodeName].push(old);
                        js_obj[nodeName].push(setJsonObj(item));
                    }

                } else {
                    if (typeof(js_obj[nodeName].push) == "undefined") {

                        var old = js_obj[nodeName];
                        js_obj[nodeName] = [];
                        js_obj[nodeName].push(old);
                    }
                    js_obj[nodeName].push(setJsonObj(item));
                }
            }
        }
        return js_obj;
    }


    var jsontoStr = function(js_obj) {
        var rejsn = JSON.stringify(js_obj, undefined, 2).replace(/(\\t|\\r|\\n)/g, '').replace(/"",[\n\t\r\s]+""[,]*/g, '').replace(/(\n[\t\s\r]*\n)/g, '').replace(/[\s\t]{2,}""[,]{0,1}/g, '').replace(/"[\s\t]{1,}"[,]{0,1}/g, '').replace(/\[[\t\s]*\]/g, '""').replace(/null,/g, '').replace(/[\\]/g, ' - ');
        return (rejsn.indexOf('"parsererror": {') == -1) ? rejsn : 'Invalid XML format';
    }
};

var xml2json = new XMLtoJSON();

I have tried to add the following code inside this js:

var fs = require('fs');
fs.writeFile("/tmp/test", "Hey there!", function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
}); 

But I haven’t been successful yet.
Right on the first line informs me that require is not set. Does anyone have any light for this case? I intend to create an html with a button and at the click of the button will generate the file.

  • require is not available in the client/browser. Not set is returned by the Node server?

  • returns in the browser console. Hummm so how should I do pro Can’t pick the variable to generate the file ?

  • So, it’s unclear, is Voce working on the client or the server? some client functions that exist on the server may be possible using webpack.

  • At the moment everything is local on my pc, I installed Ode and use it to run my application. This js is in the client, but I do not know how to make this request by the server for example.

  • I get very broad Wesley, but to try to give you a north, you will need to load the file on the front, in case the xml, right?! then send its bytes to the back (Node). With the file on the back, Voce can use the fs. To be clear, the Node File System Api you can only use on Node.

  • got Ucas, good thank you I will research a little about. There is little material still on Ode there I get a little lost but already gave me an option at least. thanks.

  • does this. first understands the difference between the front and back and just tries to send the file back. Voce can go asking for parts here that we help you.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.