edit 2 (solution)
Javascript
for a "possible" javascript way see @sabof answer below: https://stackoverflow.com/a/22172009/3309243 ..........
JQUERY
Events: I quickly made an example function getEvents(), but it seems to work :)
Attributes:
I found this somewhere on the internet, to return all attributes by using .attr()
Also I have just discovered about debugging in the browser and thus now know how to view an object. Might anyone wonder how: open your console (f12 atleast in FireFox) and type for example $("body") in the console command line (all the way at the bottom), click on the returned object and look at the right of the console :P
 //extending the attr function to return all attrs
(function($) {
      // duck-punching to make attr() return a map
      var _old = $.fn.attr;
      $.fn.attr = function() {
        var a, aLength, attributes, map;
        if (this[0] && arguments.length === 0) {
                map = {};
                attributes = this[0].attributes;
                aLength = attributes.length;
                for (a = 0; a < aLength; a++) {
                        map[attributes[a].name.toLowerCase()] = attributes[a].value;
                }
                return map;
        } else {
                return _old.apply(this, arguments);
        }
}
}(jQuery));
Thanks to the great example of @user3331198 I got the following (togheter with the code block above).
var href = "http://www.google.com/";
var text = "Google";
$( document ).ready(function() {    
   $("body").css({"background-color": "#000fff"})
       .append( $("<div>", {class: 'foo'}))
            .append($("<a>", { id: "anID", href: href, text: text }))
                     .append($("<span>").text("Foo").on("click", function(){ alert('!')}));
    loopDOM($("body"));   
 });
function loopDOM(obj,parentID=-1,thisID=0) {
   saveToDB({ 
     id: thisID,
     parent: parentID,
     tag: obj.prop("tagName"),
     style: obj.attr("style"),
     events: getEvents(obj),
     attribs: obj.attr()    
    });
    obj.children().each(function() {
       loopDOM($(this),thisID,++thisID)
    });
} 
function getEvents(obj) {   
    aEvents=[];
    if (typeof $._data( obj[0], 'events' )==="object") {
        $.each($._data( obj[0], 'events' ), function(i, event) {
           type=JSON.stringify(i);  
           aEvents[type]=[];
           $.each(event, function(j, h) {
                aEvents[type][aEvents[type].length]=h.handler;
            });
        });     
    }
    return aEvents;
}
function saveToDB(pass) {
    alert("id:"+pass["id"]+"\nparent:"+pass["parent"]+"\ntag:"+pass["tag"]+"\nstyle:"+pass["style"]);
    alert ("has the following attributes");
    for (var x in pass["attribs"])
         alert("attrib: "+x+" "+pass["attribs"][x]);
     alert ("has the following events");
    for (var x in pass["events"])
        alert("event: "+x+" "+pass["events"][x]);
 }
..........................................................................
Orginal post
I am toying around in my spare time to get a bit back into shape with coding.
For example I am building this DOM (for the practice with jQuery instead of plain HTML)
$(document).ready(function() {  
   $("body").css({"background-color": "#000fff"})
       .append( $("<div>", { class: "foo" })
            .append($("<a>", { href: href, text: text })
                .on("click", function(){ alert('!')}))
                   .append($("<span>").text("Foo")));
});
Now let's say I want to save the DOM in a DB table like this:
| ID | parentID | tag    | style      | events | atribs |
_________________________________________________________
| 0 | -1        | <body> | backgr...  | NULL   | NULL   |
| 1 |  0        | <div>  | NULL       | NULL   | class:.|
| 2 |  1        | <a>    | NULL       | click..| NULL   |
| 3 |  2        | <span> | NULL       | NULL   | NULL   |
For the purpose of retrieving and rebuilding the DOM with some JS function
To do this I would use for example some recursive loop like this: (sort of pseudo-JS)
function loopDOM(obj,parentID=-1,thisID=0) {
   saveToDB({ 
      id: thisID,
      parent: parentID,
      style: obj.getStyle,
      events: obj.getEvents,
      atribs: obj.getAtribs
   });
   obj.each(function() {
       loopDOM(child,thisID,++thisID)
   });
}
But I am totally stuck at every point here. I've been searching for at least 45 minutes but I could not answer even one of my following questions.
- How to loop through the - $('body')? how do I retrieve its children from an element? I found- .each,- .find,- .childrenbut I can't find the proper object to pass like the- <div>which is a child of- <body>.
 I can't figure out how to recursively loop through the object.
- How to get ALL the (unknown) events, attributes and CSS of an element? I haven't been able to make any of the answers I've found work. 
..........................................................................
edit 1 (outdated)
If I quickly put togheter the links @PellePenna posted I get this.. but its jquery and javascript mixed and It doenst work. I dont get the attribs nor the events. And I dont have an idea how to get an elements his position. I might be a bit retarted but I dont get the suggestions given. And also really like recursive loops :P
var el = this.getElementsByTagName("*");
for (var i = el.length; i--;) {
    //get atribs
    var nodes=[], values=[];
    if (el.attributes)
        for (var attr, i=0, attrs=el.attributes, l=attrs.length; i<l; i++){
            attr = attrs.item(i)
            nodes.push(attr.nodeName);
            values.push(attr.nodeValue);
        }
    //get events
    event=$._data(el, 'events');
}
 
     
     
     
    