1

How do I traverse an XML DOM with javscript/jquery? My xml is custom namespaced. I'm using an ajax get and it returns Document in the console. How do I parse/traverse this?

$.ajax({
    type: 'GET',
    url: 'xmlsimple.xml',
    dataType: 'xml',
    success: function(data) {
        parseXml(data);
    },
    error: function() {
        alert('fail');       
    }
  });


});

I am using jQuery 1.8 which doesn't allow you to backslash escape namespaces or use find([namespace="*"]) as far as im aware.

Rhyso
  • 696
  • 2
  • 9
  • 20

1 Answers1

1
//  
//  this might help:
//  
//      traverse.dom( 
//                    function() {
//                        // this === current_node ( inside callback )
//                        // doStuffWith( this );
//                        // return ( === )false, to break iteration
//                    }, 
//                    
//                    startNode  // provide a node to start traversing from ( it is included in traversal )
//                    
//                 );
//  
//  
//      $.ajax({
//              url       : 'xmlsimple.xml',
//              dataType  : 'xml',
//              success   : function( doc ) {
//                  traverse.dom( function() { console.log( this ); }, doc );
//              }
//            });
//  
;
( function( w, _a ) { 

     var
        pn = this.toString(), 
        F  = {}, 
        t  = !0, 
        f  = !t, 
        _s = _a.slice, 
        _d = w.document;

     function _gather_nodes( node, coll ) { 

        coll.push( node );

        node = node.firstChild;

        while ( node ) { 

            _gather_nodes( node, coll );

            node = node.nextSibling;

        }

        return coll;

     }

     _a.each = function( callback ) { 

                   return ( function ( fn, len, k ) { 

                        for ( ; k < len ; k++ ) 
                            if (
                                ( function( i ) { 
                                     return fn.call( this, i, this[i] );
                                } ).call( this, k ) === f ) break;

                        return this;

                   } ).call( this, callback, this.length, 0 );

               };

     F.dom = function( callback_fn, startNode/*, ...args*/ ) {

                  var
                     args1 = _s.call( arguments, 2 );

                  return ( _gather_nodes( ( startNode || _d ), [] ) ).each( function ( pos, N ) { 

                      return callback_fn.apply( N, args1 );

                  } );

             };





     w[pn] = F;

} ).call( new String('traverse'), window, Array.prototype );
public override
  • 974
  • 8
  • 17