i'm trying to write a function that will loop through XML/DOM and store all the node names, values (if any) and attributes (if any) into a javascript object.

here's my very basic XML file...
<?xml version="1.0" encoding="iso-8859-1"?>
        <person firstname="Bob" lastname="Jones">
        <person firstname="Larry" lastname="Smith">
        <person firstname="Wanda" lastname="Williams">
        <computer brand="Apple">
        <computer brand="Sony">
i'm passing along the XML document via AJAX (var doc = req.responseXML.documentElement).

i can step through the nodes, but not dynamically, which is what i want to do since i'm going to be manipulating multiple XML documents.

for example, this will output the first couple of levels. real basic, but not dynamic. if i had an XML file with 20 levels i'd have to make one long for() loop.
    var result = doc.nodeName;
    for (var i = 0; i < doc.childNodes.length; i++) {
        if (doc.childNodes[i].nodeType == 1) {
            result += "\n\t" + doc.childNodes[i].nodeName;
            for (var j = 0; j < doc.childNodes[i].childNodes.length; j++) {
                if (doc.childNodes[i].childNodes[j].nodeType == 1) {
                    result += "\n\t\t" + doc.childNodes[i].childNodes[j].nodeName + " (" + doc.childNodes[i].childNodes[j].childNodes.length + ")";
how can i dynamically step through the document object?