Need to construct a custom JSON file after parsing XML. To cut the long story short, have an XML file from which must be created a JSON file, but not all information from XML must be present in JSON. It must be done using only JS and Node.js. To parse XML I used xmldom and fs.
Now the part I am stuck at. To create a JSON file I can't use code snipped already available on the Web, because there are very few examples with Node.js and because I need to omit many things from XML. Looping also is not a possibility, since XMLDOM is recursive. Right now I use this code to do it:
var builder = function() {
this.meta = {};
}
builder.atRoot = function(library) {
this.meta["!name"] = library;
this.meta["!define"] = {};
this.define = this.meta["!define"];
}
builder.atNamespace = function(name) {
this.namespace = {};
this.namespace["!category"] = "namespace";
this.namespace["!description"] = "";
this.define[name] = this.namespace;
}
builder.atDescription = function(method){
this.class = {};
this.class["!Accordion"] = method;
this.class["!AccordionSection"] = method;
this.class["!ApplicationHeader"] = method;
....
}
// and so on
builder.toJson = function() {
var s = JSON.stringify(this.meta);
console.log(s);
}
But it doesn't work. Now, may be I simply don't understand how builder works, or it is the wrong way. Any ideas on what is wrong, or additional explanation of builder design pattern will be much appreciated.
Many thanks in advance.
No comments:
Post a Comment