golang – extract elements in xml string

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

golang - extract elements in xml string

问题

我想提取所有的loc元素值,但是我得到了一个空数组。

我的代码:

package main

import (
    "fmt"
    "encoding/xml"
)

type Query struct {
    XMLName xml.Name `xml:"urlset"`
    locs []Loc `xml:"url>loc"`
}

type Loc struct {
    loc string 
}

var data = []byte(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
   <loc>http://www.konga.com/mobile-recharge</loc>
   <lastmod>2015-04-14</lastmod>
   <changefreq>daily</changefreq>
   <priority>0.5</priority>
</url>
<url>
   <loc>http://www.konga.com/beauty-health-personal-care</loc>
   <lastmod>2015-04-14</lastmod>
   <changefreq>daily</changefreq>
   <priority>0.5</priority>
</url>
</urlset>`)


func main() {
    
    var q Query
    xml.Unmarshal(data, &q)
    fmt.Println(q.locs)
}
英文:

I want to extract all loc element value but I am getting an empty array

My code:

package main

import (
    &quot;fmt&quot;
    &quot;encoding/xml&quot;
)

type Query struct {
    XMLName xml.Name `xml:&quot;urlset&quot;`
    locs []Loc `xml:&quot;url&gt;loc&quot;`
}

type Loc struct {
    loc string 
}

var data = []byte(`&lt;urlset xmlns=&quot;http://www.sitemaps.org/schemas/sitemap/0.9&quot;&gt;
&lt;url&gt;
   &lt;loc&gt;http://www.konga.com/mobile-recharge&lt;/loc&gt;
   &lt;lastmod&gt;2015-04-14&lt;/lastmod&gt;
   &lt;changefreq&gt;daily&lt;/changefreq&gt;
   &lt;priority&gt;0.5&lt;/priority&gt;
&lt;/url&gt;
&lt;url&gt;
   &lt;loc&gt;http://www.konga.com/beauty-health-personal-care&lt;/loc&gt;
   &lt;lastmod&gt;2015-04-14&lt;/lastmod&gt;
   &lt;changefreq&gt;daily&lt;/changefreq&gt;
   &lt;priority&gt;0.5&lt;/priority&gt;
&lt;/url&gt;
&lt;/urlset&gt;`)


func main() {
	
	var q Query
    xml.Unmarshal(data, &amp;q)
    fmt.Println(q.locs)
}

答案1

得分: 2

只有导出的字段和Caps字段才会被解组。此外,Loc不应该是一个结构体,而可以直接是一个字符串。

package main

import (
	"encoding/xml"
	"fmt"
)

type Query struct {
	XMLName xml.Name `xml:"urlset"`
	Locs    []Loc    `xml:"url>loc"`
}

type Loc string

var data = []byte(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
   <loc>http://www.konga.com/mobile-recharge</loc>
   <lastmod>2015-04-14</lastmod>
   <changefreq>daily</changefreq>
   <priority>0.5</priority>
</url>
<url>
   <loc>http://www.konga.com/beauty-health-personal-care</loc>
   <lastmod>2015-04-14</lastmod>
   <changefreq>daily</changefreq>
   <priority>0.5</priority>
</url>
</urlset>`)

func main() {

	var q Query
	xml.Unmarshal(data, &q)
	fmt.Println(q.Locs)
}

希望对你有帮助!

英文:

It only unmarshals exported and thus Caps fields. Also Loc shouldn't be a struct but can be a string directly.

package main

import (
	&quot;encoding/xml&quot;
	&quot;fmt&quot;
)

type Query struct {
	XMLName xml.Name `xml:&quot;urlset&quot;`
	Locs    []Loc    `xml:&quot;url&gt;loc&quot;`
}

type Loc string

var data = []byte(`&lt;urlset xmlns=&quot;http://www.sitemaps.org/schemas/sitemap/0.9&quot;&gt;
&lt;url&gt;
   &lt;loc&gt;http://www.konga.com/mobile-recharge&lt;/loc&gt;
   &lt;lastmod&gt;2015-04-14&lt;/lastmod&gt;
   &lt;changefreq&gt;daily&lt;/changefreq&gt;
   &lt;priority&gt;0.5&lt;/priority&gt;
&lt;/url&gt;
&lt;url&gt;
   &lt;loc&gt;http://www.konga.com/beauty-health-personal-care&lt;/loc&gt;
   &lt;lastmod&gt;2015-04-14&lt;/lastmod&gt;
   &lt;changefreq&gt;daily&lt;/changefreq&gt;
   &lt;priority&gt;0.5&lt;/priority&gt;
&lt;/url&gt;
&lt;/urlset&gt;`)

func main() {

	var q Query
	xml.Unmarshal(data, &amp;q)
	fmt.Println(q.Locs)
}

huangapple
  • 本文由 发表于 2015年4月16日 17:22:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/29670462.html
匿名

发表评论

匿名网友

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

确定