tags:

views:

472

answers:

1

I need to create custom objects based on an xml input, the rule is that for every node, if it has a direct child node is named EndNode and the text value of which is 1, then I create a leaf object. So for every node, I need to check the direct child with name EndNode and its value. It's not so easy with the Dom API. and Dom selector(in this case I use Ext.DomQuery) doesn't have a way to select direct child of the root node... below is my attempt for using Dom selector, I need to wrap the node around with another level of node for selector to work. but I can't just say new Node(), it silently fails. I guess I have to walk through n.childNodes, but it is complicated to do it this way to check the rule I described above. Any solution?

 Ext.each(node.childNodes, function(n){
            if(n.nodeType == this.XML_NODE_ELEMENT){

                var tmp=new Node();
                console.log('hi');
                tmp.appendChild(n);
                console.log(Ext.DomQuery.select(n.tagName+">EndNode", tmp));
}
}
+2  A: 

I did an xml parser. It is quite easy with Dojo's library. Here ya are. When you're done with it though I recommend exporting the var to JSON and using it as cache.

dojo.require("dojox.xml.parser");
var parser = dojox.xml.parser;
function crules() { 
    this.rules = new Array();
    this.xml = Object;
} 
xml = '';
crules.prototype.load = function(file){
    var xmlget = dojo.xhrGet({
     url: file,
     handleAs: "xml",
     load: function(data){
      xml = data;
     },
     error: function (error) {
      console.error ('Error: ', error);
     },
     sync: true
    }
    );
    this.xml = xml;
}
crules.prototype.buildout = function (){
    var rules = this.xml.getElementsByTagName('ruleset');
    //dojo.byId('jsloading').innerHTML = 'Loading Javascript';
    for(var i=0; i<rules.length; i++){
     //dojo.byId('jsloading').innerHTML += ' .';
     r = new cruleset();
     r.name = xtagvalue(rules[i],'name');
     base = xtag(rules[i],'base');
     textcustom = xtag(rules[i],'textcustom');
     r.textcustomy = xtagvalue(textcustom[0],'y');
     r.textcustomx = xtagvalue(textcustom[0],'x');
     for(var j=0; j<base.length; j++){
      r.bases[j] = new cbase();
      r.bases[j].imgsrc = xtagvalue(base[j],'imgsrc');
      r.bases[j].color = xtagvalue(base[j],'color');
      r.bases[j].coloropts = new Array();
      var copts = xtag(rules[i],'option');
      for(var k=0; k<copts.length;k++){
       var cc = new Object();
       cc.color = xtagvalue(copts[k],'color');
       cc.imgsrc = xtagvalue(copts[k],'imgsrc');
       r.bases[j].coloropts.push(cc);
      }
     }
     zones = xtag(rules[i],'zone');
     for(var j=0; j<zones.length; j++){
      z = new czone();
      z.name =xtagvalue(zones[j],'name');
      zoneconfigs = xtag(zones[j],'zoneconfig');
      for(var n=0; n<zoneconfigs.length; n++){
       zc = new czoneconfig();
       zc.name = z.name;
       zc.x1 =xtagvalue(zones[j],'x1');
       zc.y1 =xtagvalue(zones[j],'y1');
       zc.w =xtagvalue(zones[j],'w');
       zc.h =xtagvalue(zones[j],'h');
       hotspots = xtag(zoneconfigs[n],'hotspot');
       for(var k=0; k<hotspots.length; k++){
        h = new chotspot();
        h.name = xtagvalue(hotspots[k],'name');
        h.x =xtagvalue(hotspots[k],'x');
        h.y =xtagvalue(hotspots[k],'y');
        h.nameyoffset = xtagvalue(hotspots[k],'nameyoffset');
        h.accessoryonly = xtagvalue(hotspots[k],'accessoryonly');
        if(h.accessoryonly == null){ 
         h.accessoryonly = 0;
        }
        var showname = xtag(hotspots[k],'showname');
        if(!isEmpty(showname)){
         h.showname = xtagvalue(hotspots[k],'showname');
        }
        /*h.itemset =xtagvalue(hotspots[k],'itemset');*/
        items = xtag(hotspots[k],'item');
        if(items){
         for(var l=0;l<items.length;l++){
          t = new citem();
          t.id = xtagvalue(items[l],'id');
          h.items[h.items.length] = t;
         }
        }
        zc.hotspots[zc.hotspots.length] = h;
       }
       z.zoneconfigs[z.zoneconfigs.length] = zc;
      }
      r.zones[r.zones.length] = z;
     }
     this.rules[this.rules.length] = r;
    }
    /*xmltext = parser.innerXML(xml);
      dojo.byId('cwindow').innerHTML = xmltext;*/
}

function xtag(e,tag){
    var n=null;
    n = e.getElementsByTagName(tag);
    if(n.length>=1){
     return e.getElementsByTagName(tag);
    }
    else return null;
}
function xtagvalue(e,tag){
    var n=null;
    n = e.getElementsByTagName(tag);
    if(n.length>=1){
     //console.log(tag,'here',n[0],parser.textContent(n[0]));
     return parser.textContent(n[0]);
    }
    else return null;
}
Chisum
Answer your question?
Chisum
LOL - I like how you say "quite easy" then follow that up with >100 lines of code! I hope there's an easier solution, I'm looking for one myself and will probably end up using JSON.
Karthik