go文本处理

1.xml处理

待解析的xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

go处理xml文件:

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
package main

import (
"encoding/xml"
"fmt"
"io"
"log"
"os"
)

func main() {
f, _ := os.Open("xml.xml")
defer f.Close()

data, _ := io.ReadAll(f)

bs := BookStore{}
// 将xml文件解析到对应结构体
xml.Unmarshal(data, &bs)

for _, v := range bs.Book {
fmt.Println(v)
}

// 输出配置信息到xml文件
data, _ = xml.MarshalIndent(bs, " ", " ")
f, _ = os.Create("new.xml")
defer f.Close()

f.Write(data)
}

type TitleItem struct {
Lang string `xml:"lang,attr"`
XMLValue string `xml:",innerxml"`
}

type BookItem struct {
Title TitleItem `xml:"title"`
Category string `xml:"category,attr"`
Author string `xml:"author"`
Year string `xml:"year"`
Price string `xml:"price"`
}

type BookStore struct {
XMLName xml.Name `xml:"bookstore`
Book []BookItem `xml:"book"`
}

2.json处理

待处理的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
{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "Molecule Man",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
{
"name": "Eternal Flame",
"age": 1000000,
"secretIdentity": "Unknown",
"powers": [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
}
]
}

go处理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
package main

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

func main() {
// 读取json文件
f, _ := os.Open("json.json")
defer f.Close()
data, _ := io.ReadAll(f)

sh := SuperHero{}
json.Unmarshal(data, &sh)

fmt.Println(sh)

// 输出json文件
file, _ := os.Create("new.json")
defer file.Close()
sh.SquadName = "mebaron hero"
sh.HomeTown = "shengzhen"

data, _ = json.MarshalIndent(sh, " ", " ")
// data, _ = json.Marshal(sh)
file.Write(data)

}

type SuperHero struct {
SquadName string `json:"squadName"`
HomeTown string `json:"homeTown"`
Formed string `json:"formed"`
SecretedBase string `json:"secretedBase"`
Active string `json:"active"`
Members []Member `json:"members"`
}

type Member struct {
Name string `json:"name"`
Age string `json:"age"`
SecretIdentity string `json:"secretIdentity"`
Powers []string `json:"powers"`
}