Golang 将相同级别 XML 元素解组到数组中
Golang Unmarshal same level XML elements into array
我 XML 我想将地址信息解组到它的 on 数组中作为我的结构的一部分:
<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 它将始终 return 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
}
}
如有任何帮助,我们将不胜感激。谢谢
我 XML 我想将地址信息解组到它的 on 数组中作为我的结构的一部分:
<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 它将始终 return 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
}
}
如有任何帮助,我们将不胜感激。谢谢