Go unmarshal xml into struct

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

Go unmarshal xml into struct

问题

我有这个xml文件,似乎无法将任何数据解组到结构体中。有人可以在这里帮助我吗?我以前从未使用过xml,总是更喜欢json。我刚刚用我的代码编辑了这篇帖子,但它仍然给我空的结构体值。

<Envelope>
	<Data>
		<Order>
			<DeliveryData>
				<del_country>BELGIQUE/BELGIE</del_country>
				<del_country_code>BE</del_country_code>
				<del_company>False</del_company>
				<del_name>ADAM</del_name>
				<del_contact></del_contact>
				<del_firstName></del_firstName>
				<del_addressLine1>Durasweg 33</del_addressLine1>
				<del_addressLine2></del_addressLine2>
				<del_areaCode>1000</del_areaCode>
				<del_city>BRUXELLES</del_city>
				<del_country>BE</del_country>
				<del_language>FR</del_language>
				<del_modeCode>71</del_modeCode>
				<phone1>0032872180808</phone1>
				<email></email>
				<inv_third>438802</inv_third>
				<OrderID>15787978</OrderID>
				<ParcelID>NE1578797801</ParcelID>
				<OrderDate>16/09/2014 14:22:54</OrderDate>
				<Shipping_date>16/09/2014 14:26:55</Shipping_date>
			</DeliveryData>
		</Order>
	</Data>
</Envelope>
type DeliveryData struct {
	XMLName xml.Name `xml:"DeliveryData"`
	Country string   `xml:"del_country"`
}

type Envelope struct {
	XMLName xml.Name `xml:"Envelope"`
	Data    Data     `xml:"Data"`
}

type Data struct {
	XMLName xml.Name `xml:"Data"`
	Orders  []Order  `xml:"Order"`
}

type Order struct {
	XMLName      xml.Name     `xml:"Order"`
	DeliveryData DeliveryData `xml:"DeliveryData"`
}
英文:

I have this xml file and i it seems i cant unmarshal any data into a struct. Someone can help me out here. Its just i never used xml before, always prefered json before xml. Just edited this post with my code and it still gives me empty struct values

&lt;Envelope&gt;
&lt;Data&gt;
		&lt;Order&gt;
			&lt;DeliveryData&gt;
				&lt;del_country&gt;BELGIQUE/BELGIE&lt;/del_country&gt;
				&lt;del_country_code&gt;BE&lt;/del_country_code&gt;
				&lt;del_company&gt;False&lt;/del_company&gt;
				&lt;del_name&gt;ADAM&lt;/del_name&gt;
				&lt;del_contact&gt;&lt;/del_contact&gt;
				&lt;del_firstName&gt;&lt;/del_firstName&gt;
				&lt;del_addressLine1&gt;Durasweg 33&lt;/del_addressLine1&gt;
				&lt;del_addressLine2&gt;&lt;/del_addressLine2&gt;
				&lt;del_areaCode&gt;1000&lt;/del_areaCode&gt;
				&lt;del_city&gt;BRUXELLES&lt;/del_city&gt;
				&lt;del_country&gt;BE&lt;/del_country&gt;
				&lt;del_language&gt;FR&lt;/del_language&gt;
				&lt;del_modeCode&gt;71&lt;/del_modeCode&gt;
				&lt;phone1&gt;0032872180808&lt;/phone1&gt; 
				&lt;email&gt;&lt;/email&gt;
				&lt;inv_third&gt;438802&lt;/inv_third&gt;
				&lt;OrderID&gt;15787978&lt;/OrderID&gt;
				&lt;ParcelID&gt;NE1578797801&lt;/ParcelID&gt;
				&lt;OrderDate&gt;16/09/2014 14:22:54&lt;/OrderDate&gt;
				&lt;Shipping_date&gt;16/09/2014 14:26:55&lt;/Shipping_date&gt;
			&lt;/DeliveryData&gt;
	&lt;/Order&gt;
  &lt;/Data&gt;

</Envelope>

 type DeliveryData struct {
	XMLName xml.Name `xml:&quot;DeliveryData&quot;`
	Country string   `xml:&quot;del_country&quot;`
}

type Envelope struct {
	XMLName xml.Name `xml:&quot;Envelope&quot;`
	Data    Data     `xml:&quot;Data&quot;`
}

type Data struct {
	XMLName xml.Name `xml:Data`
	Orders  []Order  `xml:Order`
}

type Order struct {
	XMLName      xml.Name     `xml:&quot;Order&quot;`
	DeliveryData DeliveryData `xml:&quot;DeliveryData&quot;`
}

答案1

得分: 7

有两个原因:

  1. 你的 XML 格式不正确 - 你应该添加一个闭合的 &lt;/Envelope&gt;
  2. 你在 Data 结构体标签中的格式不正确 - 它们没有引用属性的名称 - 这意味着 XML 反序列化器会寻找一个名为 'Order' 的字段,而不是 Orders 字段。

为了确保正确:你可以在 http://play.golang.org/p/6-odOcSOnF 找到一个完整可工作的示例。

相关部分是我的

type Data struct {
    XMLName xml.Name `xml:&quot;Data&quot;`
    Orders  []Order  `xml:&quot;Order&quot;`
}

与你原来的

type Data struct {
    XMLName xml.Name `xml:Data`
    Orders  []Order  `xml:Order`
}
英文:

There are two reasons:

  1. Your XML is malformed - you should add a closing &lt;/Envelope&gt;.
  2. Your struct tags in Data are malformed – they don't quote the name of the attribute - this means the the XML deserializer looks for an 'Order' field, instead of the Orders field.

For good measure: you can find a fully working example on http://play.golang.org/p/6-odOcSOnF

The relevant part is my

type Data struct {
    XMLName xml.Name `xml:&quot;Data&quot;`
    Orders  []Order  `xml:&quot;Order&quot;`
}

versus your original

type Data struct {
    XMLName xml.Name `xml:Data`
    Orders  []Order  `xml:Order`
}

答案2

得分: 0

根据@publysher的回答,你实际上不需要那么多的结构体,只需要两个:

type Data struct {
    XMLName xml.Name       `xml:"Envelope"`
    Data    []Fields `xml:"Data>Order>DeliveryData"`
}

type Fields struct {
    Country string   `xml:"del_country"`
    OrderID uint64
}

然后你可以解析你的XML(在你修复它之后,就像@publysher指出的那样):

var data Data
err := xml.Unmarshal([]byte(serialized), &data)
if err != nil {
    fmt.Println(err)
    return
}
英文:

Adding to @publysher's answer, you don't actually need to have so many structs, you just need 2:

type Data struct {
	XMLName xml.Name       `xml:&quot;Envelope&quot;`
	Data  []Fields `xml:&quot;Data&gt;Order&gt;DeliveryData&quot;`
}

type Fields struct {
	Country string   `xml:&quot;del_country&quot;`
	OrderID uint64
}

then you can unmarshal your xml (after you fix it of course like @publysher pointed out):

var data Data
err := xml.Unmarshal([]byte(serialized), &amp;data)
if err != nil {
	fmt.Println(err)
	return
}

huangapple
  • 本文由 发表于 2014年10月13日 16:21:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/26335607.html
匿名

发表评论

匿名网友

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

确定