将 XML 标记解析为布尔值(如果它在解组时存在)
Parse XML tag to boolean if it exists when unmarshalling
我正在尝试将 XML 标记解析为布尔值(如果存在)。 <status>
内的标签可以是 <active />
、<available />
或 <invalid />
,并且 <status>
.
内只存在这些标签之一
这是我目前的尝试:
package main
import (
"encoding/xml"
"fmt"
)
type Response struct {
XMLName xml.Name `xml:"domain"`
Authority string `xml:"authority,attr"`
RegistryType string `xml:"registryType,attr"`
EntityClass string `xml:"entityClass,attr"`
EntityName string `xml:"entityName,attr"`
DomainName string `xml:"domainName"`
Status Status `xml:"status"`
}
type Status struct {
Active bool `xml:"active,omitempty"`
Available bool `xml:"available,omitempty"`
Invalid bool `xml:"invalid,omitempty"`
}
func main() {
str := `
<domain authority="domain.fi" registryType="dchk1" entityClass="domain-name" entityName="esimerkki.fi">
<domainName>esimerkki.fi</domainName>
<status>
<active />
</status>
</domain>
`
var ans Response
err := xml.Unmarshal([]byte(str), &ans)
if err != nil {
panic(err)
}
fmt.Printf(`%#v`, ans.Status)
}
这 returns 全部 Status.*
为 false
而不是预期的 Status.Active = true
。怎样才能得到预期的结果?
第二次尝试指针:
type Status struct {
Active *bool `xml:"active,omitempty"`
Available *bool `xml:"available,omitempty"`
Invalid *bool `xml:"invalid,omitempty"`
}
*ans.Status.Active
还是false
.
正如@mh-cbon 建议的那样,只需检查指针是否为 *bool
是否为 nil
就足以确定该标记是否存在。但是我去把 XML 响应结构变成了正确的 Response
结构,它只包含需要的信息和 Status
变成了一个常量。
现在是:
// Raw XML DTO
type XmlResponse struct {
XMLName xml.Name `xml:"domain"`
Authority string `xml:"authority,attr"`
RegistryType string `xml:"registryType,attr"`
EntityClass string `xml:"entityClass,attr"`
EntityName string `xml:"entityName,attr"`
DomainName string `xml:"domainName"`
Status XmlStatus `xml:"status"`
}
// Raw XML DTO
type XmlStatus struct {
Active *bool `xml:"active,omitempty"`
Available *bool `xml:"available,omitempty"`
Invalid *bool `xml:"invalid,omitempty"`
}
// Return Response struct which doesn't have the unnecessary XML
func (x XmlResponse) GetResponse() Response {
st := Invalid // Default to invalid
if x.Status.Active != nil {
st = Active
} else if x.Status.Available != nil {
st = Available
} else if x.Status.Invalid != nil {
st = Invalid
}
return Response{
Domain: x.DomainName,
Status: st,
}
}
// Proper response parsed from XML
type Response struct {
Domain string
Status Status
}
type Status uint8
const (
Invalid Status = iota
Active
Available
)
解析为:
var xmlresp XmlResponse
err := xml.Unmarshal([]byte(str), &xmlresp)
if err != nil {
panic(err)
}
ans := xmlresp.GetResponse()
fmt.Printf(`%#v`, ans)
我正在尝试将 XML 标记解析为布尔值(如果存在)。 <status>
内的标签可以是 <active />
、<available />
或 <invalid />
,并且 <status>
.
这是我目前的尝试:
package main
import (
"encoding/xml"
"fmt"
)
type Response struct {
XMLName xml.Name `xml:"domain"`
Authority string `xml:"authority,attr"`
RegistryType string `xml:"registryType,attr"`
EntityClass string `xml:"entityClass,attr"`
EntityName string `xml:"entityName,attr"`
DomainName string `xml:"domainName"`
Status Status `xml:"status"`
}
type Status struct {
Active bool `xml:"active,omitempty"`
Available bool `xml:"available,omitempty"`
Invalid bool `xml:"invalid,omitempty"`
}
func main() {
str := `
<domain authority="domain.fi" registryType="dchk1" entityClass="domain-name" entityName="esimerkki.fi">
<domainName>esimerkki.fi</domainName>
<status>
<active />
</status>
</domain>
`
var ans Response
err := xml.Unmarshal([]byte(str), &ans)
if err != nil {
panic(err)
}
fmt.Printf(`%#v`, ans.Status)
}
这 returns 全部 Status.*
为 false
而不是预期的 Status.Active = true
。怎样才能得到预期的结果?
第二次尝试指针:
type Status struct {
Active *bool `xml:"active,omitempty"`
Available *bool `xml:"available,omitempty"`
Invalid *bool `xml:"invalid,omitempty"`
}
*ans.Status.Active
还是false
.
正如@mh-cbon 建议的那样,只需检查指针是否为 *bool
是否为 nil
就足以确定该标记是否存在。但是我去把 XML 响应结构变成了正确的 Response
结构,它只包含需要的信息和 Status
变成了一个常量。
现在是:
// Raw XML DTO
type XmlResponse struct {
XMLName xml.Name `xml:"domain"`
Authority string `xml:"authority,attr"`
RegistryType string `xml:"registryType,attr"`
EntityClass string `xml:"entityClass,attr"`
EntityName string `xml:"entityName,attr"`
DomainName string `xml:"domainName"`
Status XmlStatus `xml:"status"`
}
// Raw XML DTO
type XmlStatus struct {
Active *bool `xml:"active,omitempty"`
Available *bool `xml:"available,omitempty"`
Invalid *bool `xml:"invalid,omitempty"`
}
// Return Response struct which doesn't have the unnecessary XML
func (x XmlResponse) GetResponse() Response {
st := Invalid // Default to invalid
if x.Status.Active != nil {
st = Active
} else if x.Status.Available != nil {
st = Available
} else if x.Status.Invalid != nil {
st = Invalid
}
return Response{
Domain: x.DomainName,
Status: st,
}
}
// Proper response parsed from XML
type Response struct {
Domain string
Status Status
}
type Status uint8
const (
Invalid Status = iota
Active
Available
)
解析为:
var xmlresp XmlResponse
err := xml.Unmarshal([]byte(str), &xmlresp)
if err != nil {
panic(err)
}
ans := xmlresp.GetResponse()
fmt.Printf(`%#v`, ans)