I want to get contents of html.Node
as a string.
Example:
<div id="my-node">
<p>First paragraph</p>
<p>Second paragraph</p>
</div>
Given myNode := html.Node("#my-node")
(pseudocode), I want to retrieve entire above html as a string. Indentation does not matter.
I couldn't find anything on the internet except iterating over contents of node - myNode.NextSibling
but its over complicated and I'm pretty sure there has to be easier way.
Update: I'm reffering to golang.org/x/net/html
package.
I get what you mean, I use a lot of this in tests.
What you need is already in the same x/net/html
package - you can Render
the Node
to a bytes.Buffer
then get a string out of it:
var b bytes.Buffer
err := html.Render(&b, node)
return b.String()
Please read the doc how rendering is done on the best effort basis - but it will probably fit you.
PS. You can consult how it's used in a more real project of mine: https://github.com/wkhere/htmlx/blob/master/finder.go#L32-L39 https://github.com/wkhere/htmlx/blob/master/finder_test.go#L73