Go XML解组

huangapple go评论82阅读模式
英文:

Go xml unmarshalling

问题

type XML struct {
A Image xml:"div>div>img"
}

type Image struct {
I string xml:"src,attr"
}

英文:

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

<!-- language: lang-go -->

type XML struct {
	A Image `xml:&quot;div&gt;img&quot;`
}

type Image struct {
	I string `xml:&quot;src,attr&quot;`
}

And would be great to only declare something like this :

<!-- language: lang-go -->

type Image struct {
	I string `xml:&quot;div&gt;img,src,attr&quot;`
}

This is the HTML :

&lt;div&gt;&lt;div&gt;&lt;img src=&quot;hello.png&quot;/&gt;&lt;/div&gt;&lt;/div&gt;

答案1

得分: 1

似乎一个好的方法是使用exp/html包,像这样:

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>`

所有这些FirstChildNextSibling都是必需的,因为exp/html构建了一个“正确”的html5树,所以这段代码实际上解析了这个:

<html>
	<head></head>
	<body>
		<div>
			<div>
				<img src="hello.png"/>
			</div>
		</div>
	</body>
</html>
英文:

Seems that a good way is to use the exp/html package, like this:

<!-- language: lang-go -->

package main

import (
	&quot;exp/html&quot;
	&quot;strings&quot;
)

func main() {
	a, _ := html.Parse(strings.NewReader(testString))
	println(a.FirstChild.FirstChild.NextSibling.FirstChild.FirstChild.FirstChild.Attr[0].Val)
}

var testString = `&lt;div&gt;&lt;div&gt;&lt;img src=&quot;hello.png&quot;/&gt;&lt;/div&gt;&lt;/div&gt;`

All this FirstChild and NextSibling are needed because exp/html constructs a "correct" html5 tree so this code is actually parsing this:

&lt;html&gt;
	&lt;head&gt;&lt;/head&gt;
	&lt;body&gt;
		&lt;div&gt;
			&lt;div&gt;
				&lt;img src=&quot;hello.png&quot;/&gt;
			&lt;/div&gt;
		&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2012年9月20日 23:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/12515493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定