2

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 the node for the 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));
    }
}
3
  • 1
    This may be a really stupid, but is it possible to get the data in JSON ? Commented Nov 16, 2009 at 20:55
  • @Shawn Nope, that's not stupid. In fact, it's a good idea. Commented Nov 16, 2009 at 22:48
  • Also, for starters, I'd wrap the whole thing in try { ... } catch (e) { console.warn(e); } Commented Nov 16, 2009 at 23:16

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.