GolangNote

Golang笔记

golang 原生xml 转json

Permalink

golang 原生 xml 转 json

新建一个xml 文件 Employees.xml

XML: 测试xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0"?>
  <company>
          <staff>
                  <id>101</id>
                  <firstname>Derek</firstname>
                  <lastname>Young</lastname>
                  <username>derekyoung</username>
          </staff>
          <staff>
                  <id>102</id>
                  <firstname>John</firstname>
                  <lastname>Smith</lastname>
                  <username>johnsmith</username>
          </staff>
  </company>

go 代码

Go: xml 转json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main

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

 type jsonStaff struct {
         ID        int
         FirstName string
         LastName  string
         UserName  string
 }

 type Staff struct {
         XMLName   xml.Name `xml:"staff"`
         ID        int      `xml:"id"`
         FirstName string   `xml:"firstname"`
         LastName  string   `xml:"lastname"`
         UserName  string   `xml:"username"`
 }

 type Company struct {
         XMLName xml.Name `xml:"company"`
         Staffs  []Staff  `xml:"staff"`
 }

 func (s Staff) String() string {
         return fmt.Sprintf("\t ID : %d - FirstName : %s - LastName : %s - UserName : %s \n", s.ID, s.FirstName, s.LastName, s.UserName)
 }

 func main() {
         xmlFile, err := os.Open("Employees.xml")
         if err != nil {
                 fmt.Println("Error opening file:", err)
                 return
         }
         defer xmlFile.Close()

         XMLdata, _ := ioutil.ReadAll(xmlFile)

         var c Company
         xml.Unmarshal(XMLdata, &c)

         // sanity check - XML level
         fmt.Println(c.Staffs)

         // convert to JSON
         var oneStaff jsonStaff
         var allStaffs []jsonStaff

         for _, value := range c.Staffs {
                 oneStaff.ID = value.ID
                 oneStaff.FirstName = value.FirstName
                 oneStaff.LastName = value.LastName
                 oneStaff.UserName = value.UserName

                 allStaffs = append(allStaffs, oneStaff)
         }

         jsonData, err := json.Marshal(allStaffs)

         if err != nil {
                 fmt.Println(err)
                 os.Exit(1)
         }

         // sanity check - JSON level

         fmt.Println(string(jsonData))

         // now write to JSON file

         jsonFile, err := os.Create("./Employees.json")

         if err != nil {
                 fmt.Println(err)
         }
         defer jsonFile.Close()

         jsonFile.Write(jsonData)
         jsonFile.Close()

 }

输出

JSON: 转换输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
     {
         "ID": 101,
         "FirstName": "Derek",
         "LastName": "Young",
         "UserName": "derekyoung"
     },
     {
         "ID": 102,
         "FirstName": "John",
         "LastName": "Smith",
         "UserName": "johnsmith"
     }
 ]

也可以使用第三方库:

本文网址: https://golangnote.com/topic/139.html 转摘请注明来源

Related articles

Golang 生成防识别的图片验证码

验证码 captcha 是对抗密码强力破解、垃圾信息的有效方式,一般用于用户注册、登录,当检测到频繁发帖时也会启用验证码。下面介绍用golang 生成防机器识别的图片验证码。...

Golang Web 程序生产环境独立部署示例

一个 web 应用通常是跑在一个前端代理,如 Nginx 后,这样可以方便的在同一个服务器部署多个应用。这里说的独立部署是指让 go web 程序直接暴露在外面,独占 443、80 端口(俗称裸跑)。这样做除了性能有些提高外,更重要的是部署方便。...

Write a Comment to "golang 原生xml 转json"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms