Skip to content

Instantly share code, notes, and snippets.

@enricopolanski
Forked from jkantr/parsefile.js
Created November 30, 2017 22:13
Show Gist options
  • Save enricopolanski/110e8edf62d03cb8e3b9c3b29e9ff6d1 to your computer and use it in GitHub Desktop.
Save enricopolanski/110e8edf62d03cb8e3b9c3b29e9ff6d1 to your computer and use it in GitHub Desktop.
var fs = require("fs");
var path = require("path");
var cheerio = require("cheerio");
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;
// 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")
}
return relationship_object;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment