Golang XML转换为结构体

huangapple go评论65阅读模式
英文:

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 (
&quot;encoding/xml&quot;
&quot;fmt&quot;
&quot;io/ioutil&quot;
&quot;os&quot;
)
type ZIPResult struct {
XMLName xml.Name `xml:&quot;ZIP_result&quot;`
Text    string   `xml:&quot;,chardata&quot;`
Result  []Result `xml:&quot;result&quot;`
ADDRESSValue Address `xml:&quot;ADDRESS_value&quot;`
}
type Result struct {
Text              string `xml:&quot;,chardata&quot;`
Name              string `xml:&quot;name,attr&quot;`
Version           string `xml:&quot;version,attr&quot;`
RequestURL        string `xml:&quot;request_url,attr&quot;`
RequestZipNum     string `xml:&quot;request_zip_num,attr&quot;`
RequestZipVersion string `xml:&quot;request_zip_version,attr&quot;`
ResultCode        string `xml:&quot;result_code,attr&quot;`
ResultZipNum      string `xml:&quot;result_zip_num,attr&quot;`
ResultZipVersion  string `xml:&quot;result_zip_version,attr&quot;`
ResultValuesCount string `xml:&quot;result_values_count,attr&quot;`
}
type Address struct {
Text  string `xml:&quot;,chardata&quot;`
Value []Value`xml:&quot;value&quot;`
}
type Value struct {
Text        string `xml:&quot;,chardata&quot;`
StateKana   string `xml:&quot;state_kana,attr&quot;`
CityKana    string `xml:&quot;city_kana,attr&quot;`
AddressKana string `xml:&quot;address_kana,attr&quot;`
CompanyKana string `xml:&quot;company_kana,attr&quot;`
State       string `xml:&quot;state,attr&quot;`
City        string `xml:&quot;city,attr&quot;`
Address     string `xml:&quot;address,attr&quot;`
Company     string `xml:&quot;company,attr&quot;`
}
func main() {
// Open our xmlFile
xmlFile, err := os.Open(&quot;info.xml&quot;)
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
fmt.Println(&quot;Successfully Opened info.xml&quot;)
// 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 &#39;results&#39; which we defined above
err = xml.Unmarshal(byteValue, &amp;results)
if err != nil {
fmt.Println(err)
}
fmt.Println(results.ADDRESSValue)
}

info.xml

&lt;ZIP_result&gt;
	&lt;result name=&quot;ZipSearchXML&quot;/&gt;
	&lt;result version=&quot;1.01&quot;/&gt;
	&lt;result request_url=&quot;http%3A%2F%2Fzip.cgis.biz%2Fxml%2Fzip.php%3Fzn%3D1310045&quot;/&gt;
	&lt;result request_zip_num=&quot;1310045&quot;/&gt;
	&lt;result request_zip_version=&quot;none&quot;/&gt;
	&lt;result result_code=&quot;1&quot;/&gt;
	&lt;result result_zip_num=&quot;1310045&quot;/&gt;
	&lt;result result_zip_version=&quot;0&quot;/&gt;
	&lt;result result_values_count=&quot;1&quot;/&gt;
	&lt;ADDRESS_value&gt;
		&lt;value state_kana=&quot;トウキョウト&quot;/&gt;
		&lt;value city_kana=&quot;スミダク&quot;/&gt;
		&lt;value address_kana=&quot;オシアゲ&quot;/&gt;
		&lt;value company_kana=&quot;none&quot;/&gt;
		&lt;value state=&quot;東京都&quot;/&gt;
		&lt;value city=&quot;墨田区&quot;/&gt;
		&lt;value address=&quot;押上&quot;/&gt;
		&lt;value company=&quot;none&quot;/&gt;
	&lt;/ADDRESS_value&gt;
&lt;/ZIP_result&gt;

huangapple
  • 本文由 发表于 2022年1月19日 20:59:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/70771019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定