[英]how to Extracting data from XML
我正在從 xml 輸出中提取數據。 我寫了下面的代碼。 我只需要從下面的 xml 中提取部門編號。 在運行以下代碼時獲得空輸出。 有人可以讓我如何從 xml 中提取部門編號以及為什么我的輸出為 null?
package main
import (
"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 Users
var 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)
}
看起來提供的 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>
您可以在 Playground 上使用您提供的示例查看工作示例。 https://play.golang.org/p/4zQsaz5Z_5P
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.