Golang学习笔记--xml字符串转json

Go语言中struct和xml,struct和json之间的互相转换比较容易实现,那么xml直接转json

该如何实现?其本质还是通过struct间接转换的。让我们直接看下代码:

package main

import (
	"encoding/json"
	"encoding/xml"
	"fmt"
	"os"
)

var xmlDataRq = `<?xml version="1.0" encoding="UTF-8"?>
<request>
<head>
     <h_exch_code>880061</h_exch_code>
     <h_bank_no>0000</h_bank_no>
     <h_branch_id>B00000</h_branch_id>
     <h_fact_date>20110224</h_fact_date>
     <h_fact_time>14:49:20</h_fact_time>
     <h_exch_date>20110224</h_exch_date>
     <h_serial_no>12345678</h_serial_no>
     <h_rsp_code>1</h_rsp_code>
     <h_rsp_msg>ok</h_rsp_msg>
</head>
<body>
     <record>
            <user_id>1204300001</user_id>
            <user_pwd>e10adc3949ba59abbe56e057f20f883e</user_pwd>
            <login_ip>127.0.0.1</login_ip>
     </record>
     <record>
            <user_id>1204300002</user_id>
            <user_pwd>e10adc3949ba59abbe56e057f20f883e2</user_pwd>
            <login_ip>127.0.0.2</login_ip>
     </record>
</body>
</request>

`

type head struct {
	H_exch_code string `xml:"h_exch_code" json:"h_exch_code"`
	H_bank_no   string `xml:"h_bank_no" json:"h_bank_no"`
	H_branch_id string `xml:"h_branch_id" json:"h_branch_id"`
	H_fact_date string `xml:"h_fact_date" json:"h_fact_date"`
	H_fact_time string `xml:"h_fact_time" json:"h_fact_time"`
	H_exch_date string `xml:"h_exch_date" json:"h_exch_date"`
	H_serial_no string `xml:"h_serial_no" json:"h_serial_no"`
	H_rsp_code  string `xml:"h_rsp_code" json:"h_rsp_code"`
	H_rsp_msg   string `xml:"h_rsp_msg" json:"h_rsp_msg"`
}

type rqRecord struct {
	User_id  string `xml:"user_id" json:"user_id"`
	User_pwd string `xml:"user_pwd" json:"user_pwd"`
	Login_ip string `xml:"login_ip" json:"login_ip"`
}

type rqBody struct {
	Record []rqRecord `xml:"record" json:"record"`
}

type rspRecord struct {
	Server_code         string `xml:"server_code" json:"server_code"`
	Server_name         string `xml:"server_name" json:"server_name"`
	Trans_ip            string `xml:"trans_ip" json:"trans_ip"`
	Trans_port          string `xml:"trans_port" json:"trans_port"`
	Query_ip            string `xml:"query_ip" json:"query_ip"`
	Query_port          string `xml:"query_port" json:"query_port"`
	Broadcast_ip        string `xml:"broadcast_ip" json:"broadcast_ip"`
	Broadcast_port      string `xml:"broadcast_port" json:"broadcast_port"`
	Risk_trans_ip       string `xml:"risk_trans_ip" json:"risk_trans_ip"`
	Risk_trans_port     string `xml:"risk_trans_port" json:"risk_trans_port"`
	Risk_broadcast_ip   string `xml:"risk_broadcast_ip" json:"risk_broadcast_ip"`
	Risk_broadcast_port string `xml:"risk_broadcast_port" json:"risk_broadcast_port"`
}

type rspBody struct {
	Record []rspRecord `xml:"record" json:"record"`
}

type Request struct {
	XMLName xml.Name `xml:"request" json:"request"`
	Head    head     `xml:"head" json:"head"`
	Body    rqBody   `xml:"body" json:"body"`
}

type Response struct {
	XMLName xml.Name `xml:"response" json:"response"`
	Head    head     `xml:"head" json:"head"`
	Body    rspBody  `xml:"body" json:"body"`
}

var xmlDataRsp = `<?xml version="1.0" encoding="utf-8"?>
<response>
<head>
     <h_exch_code>880061</h_exch_code>
     <h_bank_no>0000</h_bank_no>
     <h_branch_id>B00000</h_branch_id>
     <h_fact_date>20110224</h_fact_date>
     <h_fact_time>14:49:20</h_fact_time>
     <h_exch_date>20110224</h_exch_date>
     <h_serial_no>12345678</h_serial_no>
     <h_rsp_code>HJ0000</h_rsp_code>
     <h_rsp_msg>处理成功</h_rsp_msg>
</head>
<body>
     <record>
            <server_code>M01</server_code>
            <server_name>内网 </server_name>
            <trans_ip>168.33.114.248</trans_ip>
            <trans_port>15000</trans_port>
            <query_ip>168.33.114.248</query_ip>
            <query_port>16000</query_port>
            <broadcast_ip>168.33.114.248</broadcast_ip>
            <broadcast_port>17000</broadcast_port>
            <risk_trans_ip>168.33.114.248</risk_trans_ip>
            <risk_trans_port>20000</risk_trans_port>
            <risk_broadcast_ip>168.33.114.248</risk_broadcast_ip>
            <risk_broadcast_port>21000</risk_broadcast_port>
     </record>
     <record>
            <server_code>M02</server_code>
            <server_name>外网</server_name>
            <trans_ip>210.21.197.124</trans_ip>
            <trans_port>15000</trans_port>
            <query_ip>210.21.197.124</query_ip>
            <query_port>16000</query_port>
            <broadcast_ip>210.21.197.124</broadcast_ip>
            <broadcast_port>17000</broadcast_port>
            <risk_trans_ip>210.21.197.124</risk_trans_ip>
            <risk_trans_port>20000</risk_trans_port>
            <risk_broadcast_ip>210.21.197.124</risk_broadcast_ip>
            <risk_broadcast_port>21000</risk_broadcast_port>
     </record>
</body>
</response>`

func main() {
	v := Request{}
	xml.Unmarshal([]byte(xmlDataRq), &v)

	// sanity check - XML level
	fmt.Printf("XMLName: %#v\n", v.XMLName)
	fmt.Printf("XMLName: %#v\n", v.Head)
	fmt.Printf("Body: %#v\n", v.Body)

	fmt.Println("---json")
	b, _ := json.Marshal(v)
	os.Stdout.Write(b)

	fmt.Println("------")

	v2 := Response{}
	xml.Unmarshal([]byte(xmlDataRsp), &v2)

	// sanity check - XML level
	fmt.Printf("XMLName: %#v\n", v2.XMLName)
	fmt.Printf("XMLName: %#v\n", v2.Head)
	fmt.Printf("Body: %#v\n", v2.Body)

	fmt.Println("---json")
	b2, _ := json.Marshal(v2)
	os.Stdout.Write(b2)
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值