I have a json object created with the tags and attributes of the html elements. Now that I have the json I want to rebuild the entire html running the json. Any suggestion would be of great help to me.
<div class='container'>
<div id='padre'>
<p>texto</p>
<div id='hijo'>
<h2>texto</h2>
<div id='nieto'>
</div>
<div id='nieto2'>
</div>
<div id='nieto3' style='background: red;'>
<p>texto</p>
</div>
</div>
</div>
<div id='padre'>
<div id='hijo'>
<p>texto</p>
</div>
</div>
<div id='padre'>
<div id='hijo'>
<p>texto</p>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
<script>
var obj={};
var body=document.body
var element = document.body
getAllChild(element,obj)
console.log(obj)
var json=JSON.stringify(obj);
//showObject(json)
function showObject(json){
//convertimos en json en un objeto
var obj = JSON.parse(json)
console.log(obj.children)
if(obj.children.length!=0){
for( var i = 0; i <= obj.children.length; i++ ){
document.body.innerHTML = obj.children[i].text
}
}else{
console.log('no hijos')
}
}
function getAllChild(element,obj){
var attr = [];
obj.tag=element.localName;
obj.attr=attr;
obj.name=element.name;
obj.text = element.innerText;
$(element.attributes).each(function(){
var data = {'attr':this.name, 'val':this.value }
attr.push(data)
})
if(element.children.length!=0){
obj.children=[];
[].forEach.call(element.children,(val,i)=>{
obj.children[i]={};
getAllChild(val,obj.children[i]);
});
}
}
</script>