亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 XML 中提取數據

如何從 XML 中提取數據

Go
aluckdog 2022-09-12 21:02:21
我正在努力從xml輸出中提取數據。我寫了下面的代碼。我只需要從下面的xml中提取部門編號。在運行以下代碼以獲取空輸出時。有人可以讓我如何從xml中提取部門編號以及為什么我得到空作為輸出嗎?package mainimport (    "encoding/xml"    "fmt")type Users struct {    XMLName xml.Name `xml:"users"`    User   User   `xml:"user"`}type User struct {    XMLName xml.Name `xml:"user"`     Type string  `xml:"name"`     DD  DD   `xml:"dd"`}type DD struct {    XMLName xml.Name `xml:"dd"`     Number string  `xml:"number"`    Description string  `xml:"description"`    Type  Type   `xml:"type"`    Dept  Dept   `xml:"dept"`}type Type struct{   XMLName xml.Name `xml:"type"` }type Dept struct {    XMLName xml.Name `xml:"dept"`    Number string  `xml:"number"`   Type  Type   `xml:"type"`}func main() {var users Usersvar byteValue = []byte(`<users><user><type>1</type><bu>    <number>123</number>    <id>100</id>    <type>        <code>123</code>    </type></bu><dd>    <number>1</number>    <description>abc</description>    <type>        <code>12345</code>        <id>qw123<id>    <type>    <dept>        <number>10</number>      <<<<<<<        <type>qw12345</type>            </dept></dd><bd>    <code>34we5</code>    <id>qw123<id></bd><OD>    <code>9876</code>    <id>qwerty123<id></OD>   </user></users>`)xml.Unmarshal(byteValue, &users)fmt.Println("Dept Number: " + users.User.DD.Dept.Number)}
查看完整描述

1 回答

?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

看起來提供的 XML 是錯誤的。請嘗試使用下面的 XML


<users>

    <user>

        <type>1</type>

        <bu>

            <number>123</number>

            <id>100</id>

            <type>

                <code>123</code>

            </type>

        </bu>

        <dd>

            <number>1</number>

            <description>abc</description>

            <type>

                <code>12345</code>

                <id>qw123</id>

            </type>

            <dept>

                <number>10</number>

                <type>qw12345</type>

            </dept>

        </dd>

        <bd>

            <code>34we5</code>

            <id>qw123</id>

        </bd>

        <OD>

            <code>9876</code>

            <id>qwerty123</id>

        </OD>

    </user>

</users>

您可以在操場上查看工作示例以及您提供的示例。https://play.golang.org/p/4zQsaz5Z_5P


查看完整回答
反對 回復 2022-09-12
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號