Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to deep clone JSoup Document object and get back exactly same HTML?

Is there a way to deep clone JSoup Document object and get back exactly same HTML?

I have a pre-parsed object that I want to just clone because I suspect that clone would be faster than always parsing the HTML again.

I've tried to clone it by just iterating through of all the elements of the Document object, but then I'm left without the doctype declaration and such.

P.S. I don't of course expect to get the comments...

like image 225
Bleadof Avatar asked Nov 16 '10 13:11

Bleadof


2 Answers

For any Node you can call .clone()

This was implemented in feature request.

like image 123
BalusC Avatar answered Sep 25 '22 20:09

BalusC


As I was confused by the answer and OP's comment on it, here is the result of my humble research :-).

document.clone() produces a deep copy of the document object and all of its children. It is true according to documentation and my own experiments. enter image description here

like image 36
yurin Avatar answered Sep 23 '22 20:09

yurin