登录
首页 >  Golang >  Go问答

将 XML 标记转换为布尔值

来源:stackoverflow

时间:2024-03-17 23:48:30 382浏览 收藏

在 XML 数据中,标记通常表示为布尔值,表示该标记是否存在。本文介绍了如何将 XML 标记解析为 Go 中的布尔值。作者最初尝试使用嵌套的布尔字段,但所有字段都返回 false。通过检查指针是否不为 nil,可以确定标记是否存在。作者还提出了使用常量和自定义解析器来简化和优化响应结构的解决方案。虽然这不是完美的,但它提供了一种快速而简单的黑客攻击来解决问题。

问题内容

我正在尝试将 xml 标记解析为布尔值(如果存在)。 内的标签只能是 这些标签之一存在于 内。

这是我当前的尝试:

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 := `

  esimerkki.fi
  
    
  

`

    var ans response
    err := xml.unmarshal([]byte(str), &ans)
    if err != nil {
        panic(err)
    }

    fmt.printf(`%#v`, ans.status)
}

这会将所有 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 建议的那样,只需检查指针是否不是 *boolnil 就足以确定该标记是否存在。但我将 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)

这不是一个完美的解决方案,但这是一个快速而肮脏的黑客攻击。您可以为每个项目创建结构。

type xmlstatus struct {
    active    active     `xml:"active,omitempty"`
    available available  `xml:"available,omitempty"`
    invalid   invalid    `xml:"invalid,omitempty"`
}

type active struct {
   xmlname xml.name
}

type available struct {
    xmlname xml.name
}

type invalid struct {
    xmlname xml.name 
}

然后,在后面的代码中,每个答案下面都会有一个名为 xmlname 的字段,并且该字段下也会有一个名为 local 的字段。它将包含本地 xmlname。您可以使用简单的 if 语句检查该字段。即

if ans.State.Active.XMLName.Local != "" {
    // user is active 
}

有点笨拙,但它很简单并且有效。

以上就是《将 XML 标记转换为布尔值》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>