Skip to content

Instantly share code, notes, and snippets.

@enricopolanski
Forked from jkantr/parsefile.js
Created November 30, 2017 22:13
Show Gist options
  • Select an option

  • Save enricopolanski/110e8edf62d03cb8e3b9c3b29e9ff6d1 to your computer and use it in GitHub Desktop.

Select an option

Save enricopolanski/110e8edf62d03cb8e3b9c3b29e9ff6d1 to your computer and use it in GitHub Desktop.

Revisions

  1. @jkantr jkantr revised this gist Nov 30, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion parsefile.js
    Original file line number Diff line number Diff line change
    @@ -24,5 +24,5 @@ module.exports = (mypath) => {
    console.log("Relationships parsed successfully")
    }

    return JSON.stringify(relationship_object);
    return relationship_object;
    });
  2. @jkantr jkantr revised this gist Nov 30, 2017. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions parsefile.js
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,5 @@ module.exports = (mypath) => {
    console.log("Relationships parsed successfully")
    }

    relationship_json = JSON.stringify(relationship_object);

    console.log(relationship_json);
    return JSON.stringify(relationship_object);
    });
  3. @jkantr jkantr revised this gist Nov 30, 2017. 2 changed files with 20 additions and 35 deletions.
    14 changes: 0 additions & 14 deletions node
    Original file line number Diff line number Diff line change
    @@ -1,14 +0,0 @@
    var parseFile = require("./jsxmlparser.js");

    // ReferenceError: mypath is not defined
    // at Object.<anonymous> (C:\Users\Aquazi\Desktop\Traducee\jsxmlparser.js:4:17)
    //
    // at Module._compile (module.js:624:30)
    // at Object.Module._extensions..js (module.js:635:10)
    // at Module.load (module.js:545:32)
    // at tryModuleLoad (module.js:508:12)
    // at Function.Module._load (module.js:500:3)
    // at Module.require (module.js:568:17)
    // at require (internal/module.js:11:18)
    // at repl:1:17
    // at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    41 changes: 20 additions & 21 deletions parsefile.js
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,30 @@
    var fs = require("fs");
    var path = require("path");

    module.exports=(mypath);

    var inputFile = fs.readFileSync(mypath, "utf8");

    var cheerio = require("cheerio");

    var $ = cheerio.load(inputFile, {xmlMode:true});
    module.exports = (mypath) => {
    var inputFile = fs.readFileSync(mypath, "utf8");

    var $ = cheerio.load(inputFile, {xmlMode:true});

    // defines the selector
    var relationship_number = $("Relationship").filter("[Id]").length;
    // defines the selector
    var relationship_number = $("Relationship").filter("[Id]").length;

    // Initialize an empty object
    var relationship_object = {};
    // Initialize an empty object
    var relationship_object = {};

    // loops over the relationship xml in order to find the relationship Id and the target
    // and adds the value to the relationship_object
    for (i=0; i < relationship_number; i++){
    relationship_object[$("Relationship").filter("[Id]")[i].attribs.Id] = $("Relationship").filter("[Id]")[i].attribs.Target;
    }
    // loops over the relationship xml in order to find the relationship Id and the target
    // and adds the value to the relationship_object
    for (i=0; i < relationship_number; i++){
    relationship_object[$("Relationship").filter("[Id]")[i].attribs.Id] = $("Relationship").filter("[Id]")[i].attribs.Target;
    }

    // gets a confirmation in console of correct parsing
    if(Object.keys(relationship_object).length===relationship_number){
    console.log("Relationships parsed successfully")
    }
    // gets a confirmation in console of correct parsing
    if(Object.keys(relationship_object).length===relationship_number){
    console.log("Relationships parsed successfully")
    }

    relationship_json = JSON.stringify(relationship_object);
    relationship_json = JSON.stringify(relationship_object);

    console.log(relationship_json);
    console.log(relationship_json);
    });
  4. enricopolanski created this gist Nov 30, 2017.
    14 changes: 14 additions & 0 deletions node
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    var parseFile = require("./jsxmlparser.js");

    // ReferenceError: mypath is not defined
    // at Object.<anonymous> (C:\Users\Aquazi\Desktop\Traducee\jsxmlparser.js:4:17)
    //
    // at Module._compile (module.js:624:30)
    // at Object.Module._extensions..js (module.js:635:10)
    // at Module.load (module.js:545:32)
    // at tryModuleLoad (module.js:508:12)
    // at Function.Module._load (module.js:500:3)
    // at Module.require (module.js:568:17)
    // at require (internal/module.js:11:18)
    // at repl:1:17
    // at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    31 changes: 31 additions & 0 deletions parsefile.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    var fs = require("fs");
    var path = require("path");

    module.exports=(mypath);

    var inputFile = fs.readFileSync(mypath, "utf8");

    var cheerio = require("cheerio");

    var $ = cheerio.load(inputFile, {xmlMode:true});

    // defines the selector
    var relationship_number = $("Relationship").filter("[Id]").length;

    // Initialize an empty object
    var relationship_object = {};

    // loops over the relationship xml in order to find the relationship Id and the target
    // and adds the value to the relationship_object
    for (i=0; i < relationship_number; i++){
    relationship_object[$("Relationship").filter("[Id]")[i].attribs.Id] = $("Relationship").filter("[Id]")[i].attribs.Target;
    }

    // gets a confirmation in console of correct parsing
    if(Object.keys(relationship_object).length===relationship_number){
    console.log("Relationships parsed successfully")
    }

    relationship_json = JSON.stringify(relationship_object);

    console.log(relationship_json);