Is there a way to extract the source of an image in an HTML file using only one struct (with encode/xml
)? Now I have something like this
type XML struct {
A Image `xml:"div>img"`
}
type Image struct {
I string `xml:"src,attr"`
}
And would be great to only declare something like this :
type Image struct {
I string `xml:"div>img,src,attr"`
}
This is the HTML :
<div><div><img src="hello.png"/></div></div>
Seems that a good way is to use the exp/html
package, like this:
package main
import (
"exp/html"
"strings"
)
func main() {
a, _ := html.Parse(strings.NewReader(testString))
println(a.FirstChild.FirstChild.NextSibling.FirstChild.FirstChild.FirstChild.Attr[0].Val)
}
var testString = `<div><div><img src="hello.png"/></div></div>`
All this FirstChild
and NextSibling
are needed because exp/html
constructs a "correct" html5 tree so this code is actually parsing this:
<html>
<head></head>
<body>
<div>
<div>
<img src="hello.png"/>
</div>
</div>
</body>
</html>