How to unmarshal Go xml?

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

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 := `&lt;products updated=&quot;9/1/2017&quot;&gt;
&lt;product name=&quot;o365&quot;&gt;
&lt;addresslist type=&quot;IPv4&quot;&gt;
&lt;address&gt;13.64.196.27/32&lt;/address&gt;
&lt;address&gt;13.64.198.19/32&lt;/address&gt;
&lt;/addresslist&gt;
&lt;/product&gt;
&lt;/products&gt;`

	type Azure struct {
		XMLName  xml.Name  `xml:&quot;products&quot;`
		Products []Product `xml:&quot;product&quot;`
	}

	type Product struct {
		XMLName xml.Name `xml:&quot;product&quot;`
		Name    string   `xml:&quot;name,attr&quot;`
		List    []List   `xml:&quot;addresslist&quot;`
	}

	type List struct {
		XMLName xml.Name `xml:&quot;addresslist&quot;`
		Type    string   `xml:&quot;type,attr&quot;`
		Address []string `xml:&quot;addressList&gt;address&quot;`
	}

	var products Azure
	xml.Unmarshal([]byte(data), &amp;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 (
	&quot;fmt&quot;
	&quot;encoding/xml&quot;
)

type Azure struct {
	XMLName  xml.Name  `xml:&quot;products&quot;`
	Products []Product `xml:&quot;product&quot;`
}

type Product struct {
	XMLName xml.Name `xml:&quot;product&quot;`
	Name    string   `xml:&quot;name,attr&quot;`
	List    []List   `xml:&quot;addresslist&quot;`
}

type List struct {
	XMLName xml.Name `xml:&quot;addresslist&quot;`
	Type    string   `xml:&quot;type,attr&quot;`
	Address []string `xml:&quot;addressList&gt;address&quot;`
}

func main() {
	data := `&lt;products updated=&quot;9/1/2017&quot;&gt;
&lt;product name=&quot;o365&quot;&gt;
&lt;addresslist type=&quot;IPv4&quot;&gt;
&lt;address&gt;13.64.196.27/32&lt;/address&gt;
&lt;address&gt;13.64.198.19/32&lt;/address&gt;
&lt;/addresslist&gt;
&lt;/product&gt;
&lt;/products&gt;`

	var products Azure
	xml.Unmarshal([]byte(data), &amp;products)
	fmt.Println(products)
}

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

发表评论

匿名网友

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

确定