取消编组嵌套 xml 戈朗

我想将嵌套的 xml 解封为戈朗结构。我想要取消编组的 xml 如下所示。


<?xml version="1.0" encoding="UTF-8"?>

<status>

    <authorized>true</authorized>

    <plan>Basic</plan>

    <usage_reports>

        <usage_report metric="hits" period="day">

            <period_start>2021-07-20 00:00:00 +0000</period_start>

            <period_end>2021-07-21 00:00:00 +0000</period_end>

            <max_value>1000000</max_value>

            <current_value>0</current_value>

        </usage_report>

        <usage_report metric="hits.82343" period="day">

            <period_start>2021-07-20 00:00:00 +0000</period_start>

            <period_end>2021-07-21 00:00:00 +0000</period_end>

            <max_value>1000000</max_value>

            <current_value>0</current_value>

        </usage_report>

    </usage_reports>

</status>

为了取消马歇尔,我定义了两个围棋结构,如下所示。


// UsageReport represents the usage report of a particular metric.

type UsageReport struct {

    Usage   string `xml:"usage_report,chardata"`

    Metric  string `xml:"metric,attr"`

    Period  string `xml:"period,attr"`

    Start   string `xml:"period_start"`

    End     string `xml:"period_end"`

    Max     int64  `xml:"max_value"`

    Current int64  `xml:"current_value"`

}


// AuthResponse represents the structure of the response from authorize calls.

type AuthResponse struct {

    Status     xml.Name      `xml:"status"`

    Authorized bool          `xml:"authorized"`

    Plan       string        `xml:"plan"`

    Usages     []UsageReport `xml:"usage_reports"`

}

但是当我尝试使用 解组时,只有 和 值被取消组。解组的结果如下所示。xml.UnmarshalPlanAuthorized


XML: {{ } true Basic []}


互换的青春
浏览 155回答 2
2回答

小唯快跑啊

试试这个游乐场。我使用此工具作弊并帮助我从XML数据生成Go类型。package mainimport (&nbsp; &nbsp; "encoding/xml"&nbsp; &nbsp; "fmt")type Status struct {&nbsp; &nbsp; XMLName&nbsp; &nbsp; &nbsp; xml.Name `xml:"status"`&nbsp; &nbsp; Text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp;`xml:",chardata"`&nbsp; &nbsp; Authorized&nbsp; &nbsp;string&nbsp; &nbsp;`xml:"authorized"`&nbsp; &nbsp; Plan&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp;`xml:"plan"`&nbsp; &nbsp; UsageReports struct {&nbsp; &nbsp; &nbsp; &nbsp; Text&nbsp; &nbsp; &nbsp; &nbsp; string `xml:",chardata"`&nbsp; &nbsp; &nbsp; &nbsp; UsageReport []struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string `xml:",chardata"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Metric&nbsp; &nbsp; &nbsp; &nbsp;string `xml:"metric,attr"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Period&nbsp; &nbsp; &nbsp; &nbsp;string `xml:"period,attr"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PeriodStart&nbsp; string `xml:"period_start"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PeriodEnd&nbsp; &nbsp; string `xml:"period_end"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MaxValue&nbsp; &nbsp; &nbsp;string `xml:"max_value"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CurrentValue string `xml:"current_value"`&nbsp; &nbsp; &nbsp; &nbsp; } `xml:"usage_report"`&nbsp; &nbsp; } `xml:"usage_reports"`}func main() {&nbsp; &nbsp; status := Status{}&nbsp; &nbsp; err := xml.Unmarshal([]byte(xmlStr), &status)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(status.UsageReports.UsageReport[0].MaxValue)}const xmlStr = `<?xml version="1.0" encoding="UTF-8"?><status><authorized>true</authorized><plan>Basic</plan><usage_reports><usage_report metric="hits" period="day"><period_start>2021-07-20 00:00:00 +0000</period_start><period_end>2021-07-21 00:00:00 +0000</period_end><max_value>1000000</max_value><current_value>0</current_value></usage_report><usage_report metric="hits.82343" period="day"><period_start>2021-07-20 00:00:00 +0000</period_start><period_end>2021-07-21 00:00:00 +0000</period_end><max_value>1000000</max_value><current_value>0</current_value></usage_report></usage_reports></status>`

手掌心

为了获得字段的值,请根据姆科普里瓦的建议进行修改。Usagesxml:"usage_reports"xml:"usage_reports>usage_report"package mainimport (&nbsp; &nbsp; "encoding/xml"&nbsp; &nbsp; "fmt")type UsageReport struct {&nbsp; &nbsp; Metric&nbsp; string `xml:"metric,attr"`&nbsp; &nbsp; Period&nbsp; string `xml:"period,attr"`&nbsp; &nbsp; Start&nbsp; &nbsp;string `xml:"period_start"`&nbsp; &nbsp; End&nbsp; &nbsp; &nbsp;string `xml:"period_end"`&nbsp; &nbsp; Max&nbsp; &nbsp; &nbsp;int64&nbsp; `xml:"max_value"`&nbsp; &nbsp; Current int64&nbsp; `xml:"current_value"`}type AuthResponse struct {&nbsp; &nbsp; Status&nbsp; &nbsp; &nbsp;xml.Name&nbsp; &nbsp; &nbsp; `xml:"status"`&nbsp; &nbsp; Authorized bool&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `xml:"authorized"`&nbsp; &nbsp; Plan&nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; `xml:"plan"`&nbsp; &nbsp; Usages&nbsp; &nbsp; &nbsp;[]UsageReport `xml:"usage_reports>usage_report"`}var data = []byte(`<?xml version="1.0" encoding="UTF-8"?><status>&nbsp; &nbsp; <authorized>true</authorized>&nbsp; &nbsp; <plan>Basic</plan>&nbsp; &nbsp; <usage_reports>&nbsp; &nbsp; &nbsp; &nbsp; <usage_report metric="hits" period="day">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <period_start>2021-07-20 00:00:00 +0000</period_start>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <period_end>2021-07-21 00:00:00 +0000</period_end>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <max_value>1000000</max_value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <current_value>0</current_value>&nbsp; &nbsp; &nbsp; &nbsp; </usage_report>&nbsp; &nbsp; &nbsp; &nbsp; <usage_report metric="hits.82343" period="day">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <period_start>2021-07-20 00:00:00 +0000</period_start>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <period_end>2021-07-21 00:00:00 +0000</period_end>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <max_value>1000000</max_value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <current_value>0</current_value>&nbsp; &nbsp; &nbsp; &nbsp; </usage_report>&nbsp; &nbsp; </usage_reports></status>`)func main() {&nbsp; &nbsp; r := AuthResponse{}&nbsp; &nbsp; if err := xml.Unmarshal(data, &r); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(r)}输出:{{ } true Basic [{hits day 2021-07-20 00:00:00 +0000 2021-07-21 00:00:00 +0000 1000000 0} {hits.82343 day 2021-07-20 00:00:00 +0000 2021-07-21 00:00:00 +0000 1000000 0}]}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go