英文:
Golang Unmarshal same level XML elements into array
问题
我有一个XML文件,我想将地址信息解析为结构体中的一个数组:
<customer>
...
<dob>1990-10-01</dob>
<address1>555 Hollywood Blvd</address1>
<city>Hollywood</city>
<state>CA</state>
<zipCode>99999</zipCode>
<alternateAddress1>575 Hollywood St</alternateAddress1>
<alternateCity>Los Angeles</alternateCity>
<alternateState>CA</alternateState>
<alternateZipCode>12345</alternateZipCode>
....
</customer>
我尝试的结构体如下:
type Test struct {
CustProfile struct {
DOB string `xml:"birthDate" json:"dob"`
Address []struct {
PrimaryAddress struct {
Street string `xml:"address1" json:"line1"`
City string `xml:"city" json:"city"`
State string `xml:"state" json:"state"`
ZipCode string `xml:"zipCode" json:"zip"`
IsPrimaryAddress string `json:"isPrimaryAddress"`
}
AlternateAddress struct {
Street string `xml:"alternateAddress1" json:"line1"`
City string `xml:"alternateCity" json:"city"`
State string `xml:"alternateState" json:"state"`
ZipCode string `xml:"alternateZipCode" json:"zip"`
IsPrimaryAddress string `json:"isPrimaryAddress"`
}
} `json:"address"`
} `xml:"customer" json:"custProfile"`
}
我遇到了几个问题。第一个问题是所有元素都在同一级别上,根据我目前的了解,由于CustProfile结构体已经与customer父元素关联,我无法在Address结构体内引用任何子元素,因为路径将是customer->?->city,state,etc,这将始终返回null,因为该路径不存在。
第二个问题是我尝试为IsPrimaryAddress定义一个默认字符串的方式。我尝试了以下代码,但出现了未定义错误:
var marshalTest Test
...
marshalTest.CustProfile.Address.PrimaryAddress.IsPrimaryAddress = "Y"
marshalTest.CustProfile.Address.AlternateAddress.IsPrimaryAddress = "N"
是否可能将此XML解析为以下结构体?
{
"custProfile": {
"dob": "1990-10-01",
"address": [
{
"line1": "555 Hollywood Blvd",
"city": "HOLLYWOOD",
"state": "CA",
"zip": "99999",
"isPrimaryAddress": "Y"
},
{
"line1": "575 Hollywood St",
"city": "LOS ANGELES",
"state": "CA",
"zip": "12345",
"isPrimaryAddress": "N"
}
]
}
}
我对Go中的XML编码并不太熟悉,我能做到的最好的是:
{
"custProfile": {
"dob": "1990-10-01",
"address": null
}
}
非常感谢您的帮助。
英文:
I have XML that I would like to unmarshal the address info into its on array as part of my struct:
<customer>
...
<dob>1990-10-01</dob>
<address1>555 Hollywood Blvd</address1>
<city>Hollywood</city>
<state>CA</state>
<zipCode>99999</zipCode>
<alternateAddress1>575 Hollywood St</alternateAddress1>
<alternateCity>Los Angeles</alternateCity>
<alternateState>CA</alternateState>
<alternateZipCode>12345</alternateZipCode>
....
</customer>
My attempt at a struct:
type Test struct {
CustProfile struct {
DOB string `xml:"birthDate" json:"dob"`
Address []struct {
PrimaryAddress struct {
Street string `xml:"address1" json:"line1"`
City string `xml:"city" json:"city"`
State string `xml:"state" json:"state"`
ZipCode string `xml:"zipCode" json:"zip"`
IsPrimaryAddress string `json:"isPrimaryAddress"`
}
AlternateAddress struct {
Street string `xml:"alternateAddress1" json:"line1"`
City string `xml:"alternateCity" json:"city"`
State string `xml:"alternateState" json:"state"`
ZipCode string `xml:"alternateZipCode" json:"zip"`
IsPrimaryAddress string `json:"isPrimaryAddress"`
}
} `json:"address"`
} `xml:"customer" json:"custProfile"`
}
So I'm having a couple of issues here. The first issue is that all the elements are in the same level and from what I figured out so far, since the CustProfile struct is already associated with the customer parent element, I'm not able to reference any child elements inside the Address struct because the path would be customer->?>city,state,etc which will always return null since that path doesn't exist.
The second issue is the way I'm trying to define a default string for IsPrimaryAddress. I tried doing something like this but I'm getting an undefined error.
var marshalTest Test
...
marshalTest.CustProfile.Address.PrimaryAddress.IsPrimaryAddress = "Y";
marshalTest.CustProfile.Address.AlternateAddress.IsPrimaryAddress = "N";
Is it possible to unmarshal this XML into a struct that results in the struct below?
{
"custProfile": {
"dob": "1990-10-01",
"address": [
{
"line1": "555 Hollywood Blvd",
"city": "HOLLYWOOD",
"state": "CA",
"zip": "99999",
"isPrimaryAddress": "Y"
},
{
"line1": "575 Hollywood St",
"city": "LOS ANGELES",
"state": "CA",
"zip": "12345",
"isPrimaryAddress": "N"
}
]
}
I'm not at all that familiar with XML encoding in Go and the best I've been able to manage is this:
{
"custProfile": {
"dob": "1990-10-01",
"address": null
}
}
Any help would be greatly appreciated. Thanks
答案1
得分: 0
我能够使用 XML 解析和 Xquery 以及 JSON 解析和 Gabs 的组合来手动构建我想要的结构。可能不是最干净的解决方案,但它允许我按照我想要的程度自定义结构。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论