英文:
How to unmarshal Go xml?
问题
我有一个XML数据需要解析成字符串切片["13.64.196.27/32", "13.64.198.19/32"],但是在一开始就出现了错误"undefined: Product"。我已经定义了Product结构体...不确定它需要我提供什么。请参考下面的代码和play.golang.org/p/Ak6bx3BLwq
func main() {
data := `<products updated="9/1/2017">
<product name="o365">
<addresslist type="IPv4">
<address>13.64.196.27/32</address>
<address>13.64.198.19/32</address>
</addresslist>
</product>
</products>`
type Azure struct {
XMLName xml.Name `xml:"products"`
Products []Product `xml:"product"`
}
type Product struct {
XMLName xml.Name `xml:"product"`
Name string `xml:"name,attr"`
List []List `xml:"addresslist"`
}
type List struct {
XMLName xml.Name `xml:"addresslist"`
Type string `xml:"type,attr"`
Address []string `xml:"address"`
}
var products Azure
xml.Unmarshal([]byte(data), &products)
fmt.Println(products.List.Address)
}
英文:
I have xml data to unmarshal into slice of strings ["13.64.196.27/32", "13.64.198.19/32"] but getting error "undefined: Product"at the very beginning of it all. I have Product struct defined...not sure what it wants from me. See below and play.golang.org/p/Ak6bx3BLwq
func main() {
data := `<products updated="9/1/2017">
<product name="o365">
<addresslist type="IPv4">
<address>13.64.196.27/32</address>
<address>13.64.198.19/32</address>
</addresslist>
</product>
</products>`
type Azure struct {
XMLName xml.Name `xml:"products"`
Products []Product `xml:"product"`
}
type Product struct {
XMLName xml.Name `xml:"product"`
Name string `xml:"name,attr"`
List []List `xml:"addresslist"`
}
type List struct {
XMLName xml.Name `xml:"addresslist"`
Type string `xml:"type,attr"`
Address []string `xml:"addressList>address"`
}
var products Azure
xml.Unmarshal([]byte(data), &products)
fmt.PrintLn(products.List.Address)
}
答案1
得分: 2
首先,你应该在函数实现之外定义变量,其次,你试图使用不存在的 fmt.PrintLn
。
我稍作修改,希望能有所帮助:
package main
import (
"fmt"
"encoding/xml"
)
type Azure struct {
XMLName xml.Name `xml:"products"`
Products []Product `xml:"product"`
}
type Product struct {
XMLName xml.Name `xml:"product"`
Name string `xml:"name,attr"`
List []List `xml:"addresslist"`
}
type List struct {
XMLName xml.Name `xml:"addresslist"`
Type string `xml:"type,attr"`
Address []string `xml:"addressList>address"`
}
func main() {
data := `<products updated="9/1/2017">
<product name="o365">
<addresslist type="IPv4">
<address>13.64.196.27/32</address>
<address>13.64.198.19/32</address>
</addresslist>
</product>
</products>`
var products Azure
xml.Unmarshal([]byte(data), &products)
fmt.Println(products)
}
英文:
Firstly you should define variables out of function implementations and secondly, you're trying to use fmt.PrintLn
which doesn't exist.
I've fixed a little, hope it helps:
package main
import (
"fmt"
"encoding/xml"
)
type Azure struct {
XMLName xml.Name `xml:"products"`
Products []Product `xml:"product"`
}
type Product struct {
XMLName xml.Name `xml:"product"`
Name string `xml:"name,attr"`
List []List `xml:"addresslist"`
}
type List struct {
XMLName xml.Name `xml:"addresslist"`
Type string `xml:"type,attr"`
Address []string `xml:"addressList>address"`
}
func main() {
data := `<products updated="9/1/2017">
<product name="o365">
<addresslist type="IPv4">
<address>13.64.196.27/32</address>
<address>13.64.198.19/32</address>
</addresslist>
</product>
</products>`
var products Azure
xml.Unmarshal([]byte(data), &products)
fmt.Println(products)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论