英文:
Golang XML to Struct
问题
我可以帮你翻译这段内容。以下是翻译的结果:
我想用Golang解析这个XML,但我不知道如何定义结构体。
type ZIPResult struct {
Result []Result `xml:"result"`
AddressValue AddressValue `xml:"ADDRESS_value"`
}
type Result struct {
Name string `xml:"name,attr"`
Version string `xml:"version,attr"`
RequestURL string `xml:"request_url,attr"`
RequestZipNum string `xml:"request_zip_num,attr"`
RequestZipVersion string `xml:"request_zip_version,attr"`
ResultCode string `xml:"result_code,attr"`
ResultZipNum string `xml:"result_zip_num,attr"`
ResultZipVersion string `xml:"result_zip_version,attr"`
ResultValuesCount string `xml:"result_values_count,attr"`
}
type AddressValue struct {
Value []Value `xml:"value"`
}
type Value struct {
StateKana string `xml:"state_kana,attr"`
CityKana string `xml:"city_kana,attr"`
AddressKana string `xml:"address_kana,attr"`
CompanyKana string `xml:"company_kana,attr"`
State string `xml:"state,attr"`
City string `xml:"city,attr"`
Address string `xml:"address,attr"`
Company string `xml:"company,attr"`
}
有人可以教我吗?
英文:
I want to unmarshal this xml with golang, but I don't know how to define struct.
<ZIP_result>
<result name="ZipSearchXML"/>
<result version="1.01"/>
<result request_url="http%3A%2F%2Fzip.cgis.biz%2Fxml%2Fzip.php%3Fzn%3D1310045"/>
<result request_zip_num="1310045"/>
<result request_zip_version="none"/>
<result result_code="1"/>
<result result_zip_num="1310045"/>
<result result_zip_version="0"/>
<result result_values_count="1"/>
<ADDRESS_value>
<value state_kana="トウキョウト"/>
<value city_kana="スミダク"/>
<value address_kana="オシアゲ"/>
<value company_kana="none"/>
<value state="東京都"/>
<value city="墨田区"/>
<value address="押上"/>
<value company="none"/>
</ADDRESS_value>
</ZIP_result>
Can anyone teach me?
答案1
得分: 1
希望这对你有帮助。
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
type ZIPResult struct {
XMLName xml.Name `xml:"ZIP_result"`
Text string `xml:",chardata"`
Result []Result `xml:"result"`
ADDRESSValue Address `xml:"ADDRESS_value"`
}
type Result struct {
Text string `xml:",chardata"`
Name string `xml:"name,attr"`
Version string `xml:"version,attr"`
RequestURL string `xml:"request_url,attr"`
RequestZipNum string `xml:"request_zip_num,attr"`
RequestZipVersion string `xml:"request_zip_version,attr"`
ResultCode string `xml:"result_code,attr"`
ResultZipNum string `xml:"result_zip_num,attr"`
ResultZipVersion string `xml:"result_zip_version,attr"`
ResultValuesCount string `xml:"result_values_count,attr"`
}
type Address struct {
Text string `xml:",chardata"`
Value []Value `xml:"value"`
}
type Value struct {
Text string `xml:",chardata"`
StateKana string `xml:"state_kana,attr"`
CityKana string `xml:"city_kana,attr"`
AddressKana string `xml:"address_kana,attr"`
CompanyKana string `xml:"company_kana,attr"`
State string `xml:"state,attr"`
City string `xml:"city,attr"`
Address string `xml:"address,attr"`
Company string `xml:"company,attr"`
}
func main() {
// 打开我们的 xmlFile
xmlFile, err := os.Open("info.xml")
// 如果 os.Open 返回一个错误,则处理它
if err != nil {
fmt.Println(err)
}
fmt.Println("成功打开 info.xml")
// 延迟关闭 xmlFile,以便我们稍后解析它
defer xmlFile.Close()
byteValue, _ := ioutil.ReadAll(xmlFile)
// 初始化我们的 ZIPResult 结构体
var results ZIPResult
// 将包含 xml 文件内容的 byteArray 解组为我们上面定义的 'results'
err = xml.Unmarshal(byteValue, &results)
if err != nil {
fmt.Println(err)
}
fmt.Println(results.ADDRESSValue)
}
info.xml
<ZIP_result>
<result name="ZipSearchXML"/>
<result version="1.01"/>
<result request_url="http%3A%2F%2Fzip.cgis.biz%2Fxml%2Fzip.php%3Fzn%3D1310045"/>
<result request_zip_num="1310045"/>
<result request_zip_version="none"/>
<result result_code="1"/>
<result result_zip_num="1310045"/>
<result result_zip_version="0"/>
<result result_values_count="1"/>
<ADDRESS_value>
<value state_kana="トウキョウト"/>
<value city_kana="スミダク"/>
<value address_kana="オシアゲ"/>
<value company_kana="none"/>
<value state="東京都"/>
<value city="墨田区"/>
<value address="押上"/>
<value company="none"/>
</ADDRESS_value>
</ZIP_result>
英文:
I hope you find this helpful.
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
type ZIPResult struct {
XMLName xml.Name `xml:"ZIP_result"`
Text string `xml:",chardata"`
Result []Result `xml:"result"`
ADDRESSValue Address `xml:"ADDRESS_value"`
}
type Result struct {
Text string `xml:",chardata"`
Name string `xml:"name,attr"`
Version string `xml:"version,attr"`
RequestURL string `xml:"request_url,attr"`
RequestZipNum string `xml:"request_zip_num,attr"`
RequestZipVersion string `xml:"request_zip_version,attr"`
ResultCode string `xml:"result_code,attr"`
ResultZipNum string `xml:"result_zip_num,attr"`
ResultZipVersion string `xml:"result_zip_version,attr"`
ResultValuesCount string `xml:"result_values_count,attr"`
}
type Address struct {
Text string `xml:",chardata"`
Value []Value`xml:"value"`
}
type Value struct {
Text string `xml:",chardata"`
StateKana string `xml:"state_kana,attr"`
CityKana string `xml:"city_kana,attr"`
AddressKana string `xml:"address_kana,attr"`
CompanyKana string `xml:"company_kana,attr"`
State string `xml:"state,attr"`
City string `xml:"city,attr"`
Address string `xml:"address,attr"`
Company string `xml:"company,attr"`
}
func main() {
// Open our xmlFile
xmlFile, err := os.Open("info.xml")
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened info.xml")
// defer the closing of our xmlFile so that we can parse it later on
defer xmlFile.Close()
byteValue, _ := ioutil.ReadAll(xmlFile)
// we initialize our Users array
var results ZIPResult
// we unmarshal our byteArray which contains our
// xmlFiles content into 'results' which we defined above
err = xml.Unmarshal(byteValue, &results)
if err != nil {
fmt.Println(err)
}
fmt.Println(results.ADDRESSValue)
}
info.xml
<ZIP_result>
<result name="ZipSearchXML"/>
<result version="1.01"/>
<result request_url="http%3A%2F%2Fzip.cgis.biz%2Fxml%2Fzip.php%3Fzn%3D1310045"/>
<result request_zip_num="1310045"/>
<result request_zip_version="none"/>
<result result_code="1"/>
<result result_zip_num="1310045"/>
<result result_zip_version="0"/>
<result result_values_count="1"/>
<ADDRESS_value>
<value state_kana="トウキョウト"/>
<value city_kana="スミダク"/>
<value address_kana="オシアゲ"/>
<value company_kana="none"/>
<value state="東京都"/>
<value city="墨田区"/>
<value address="押上"/>
<value company="none"/>
</ADDRESS_value>
</ZIP_result>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论