Skip to content

Instantly share code, notes, and snippets.

@enricopolanski
Created November 30, 2017 21:33
Show Gist options
  • Save enricopolanski/9799d6bbe37000207fe5f9bd90ad943a to your computer and use it in GitHub Desktop.
Save enricopolanski/9799d6bbe37000207fe5f9bd90ad943a to your computer and use it in GitHub Desktop.
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)
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment