英文:
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
<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"`
}
答案1
得分: 7
有两个原因:
- 你的 XML 格式不正确 - 你应该添加一个闭合的
</Envelope>
。 - 你在
Data
结构体标签中的格式不正确 - 它们没有引用属性的名称 - 这意味着 XML 反序列化器会寻找一个名为 'Order' 的字段,而不是Orders
字段。
为了确保正确:你可以在 http://play.golang.org/p/6-odOcSOnF 找到一个完整可工作的示例。
相关部分是我的
type Data struct {
XMLName xml.Name `xml:"Data"`
Orders []Order `xml:"Order"`
}
与你原来的
type Data struct {
XMLName xml.Name `xml:Data`
Orders []Order `xml:Order`
}
英文:
There are two reasons:
- Your XML is malformed - you should add a closing
</Envelope>
. - 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 theOrders
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:"Data"`
Orders []Order `xml:"Order"`
}
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:"Envelope"`
Data []Fields `xml:"Data>Order>DeliveryData"`
}
type Fields struct {
Country string `xml:"del_country"`
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), &data)
if err != nil {
fmt.Println(err)
return
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论