ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Go

Go lang์œผ๋กœ xml parser ๊ตฌํ˜„

๋Œ•๋Œ•์ด๋ฐœ๐Ÿพ 2021. 3. 20. 17:30

golang

 

๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋„ ํ”ํžˆ ํ•  ์ˆ˜ ์žˆ๋Š” XML ํŒŒ์ผ Parser๋ฅผ Golang ์„ ๊ฐ€์ง€๊ณ  ๊ตฌํ˜„ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

์ „ ๋ณดํ†ต Node.js๋‚˜ ์ˆœ์ˆ˜ javascript์˜ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด ์†Œ๋Ÿ‰์˜ XML ํŒŒ์ผ์„ ํŒŒ์‹ฑํ•˜์—ฌ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ 

๋Œ€๋ž‘์˜ XML ํŒŒ์ผ ๋ถ„์„์„ ํ•˜๊ธฐ์—๋Š” ์‹ฑ๊ธ€์Šค๋ ˆ๋“œ ๊ธฐ๋ฐ˜์ธ Node.js ์˜ ์ฒ˜๋ฆฌ๋ฐฉ์‹์ด ๋ถ€์ ํ•ฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์—ฌ 

์ถ”ํ›„์—๋Š” Go ์˜ ์žฅ์ ์ธ ๊ณ ๋ฃจํ‹ด์„ ํ™œ์šฉํ•œ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์ˆ˜์˜ ํŒŒ์ผ XML ํŒŒ์ผ ํŒŒ์‹ฑ์„ ๊ตฌํ˜„ํ•ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

 

์•„์ง Golang ์— ๋Œ€ํ•œ ์‚ฌ์šฉ๋ฒ•์ด ์ต์ˆ™์น˜์•Š์•„ ์ด ์†Œ์Šค์ฝ”๋“œ๋Š” ๋‹จ๊ฑด XML ํŒŒ์ผ์— ๋Œ€ํ•œ ํŒŒ์‹ฑ์„ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

 

๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ

์†Œ์Šค์ฝ”๋“œ ๊ตฌ์„ฑ์€ mainํŒŒ์ผ๊ณผ parser ํŒŒ์ผ๋กœ ๊ต‰์žฅํžˆ ๋‹จ์ˆœํ•ฉ๋‹ˆ๋‹ค. main ์ฝ”๋“œ์—์„œ parser ์ฝ”๋“œ์— ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

 

 

 

[utils/parser.go]

package utils

import (
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
)

type Gpx struct {
	XMLName xml.Name `xml:"gpx"`
	Trk     Trk      `xml:"trk"`
}

type Trk struct {
	XMLName xml.Name `xml:"trk"`
	Name    Name     `xml:"name"`
	Trkseg  Trkseg   `xml:"trkseg"`
}

type Name struct {
	Name string `xml:"name"`
}

type Trkseg struct {
	XMLName xml.Name `xml:"trkseg"`
	Trkpt   []Trkpt  `xml:"trkpt"`
}

type Trkpt struct {
	XMLName xml.Name `xml:"trkpt"`
	Lat     string   `xml:"lat,attr"`
	Lon     string   `xml:"lon,attr"`
	Ele     string   `xml:"ele"`
	Time    string   `xml:"time"`
}

func Run() {
	// ํ”„๋กœ์ ํŠธ root ๊ฒฝ๋กœ ๊ธฐ์ค€
	p := filepath.Join("./files", filepath.Base("20210320143858.gpx"))
	fp, err := os.Open(p)
	if err != nil {
		panic(err)
	}
	defer fp.Close()

	// xml ํŒŒ์ผ ์ฝ๊ธฐ
	data, err := ioutil.ReadAll(fp)

	// xml ๋””์ฝ”๋”ฉ
	var Gpx Gpx
	xmlerr := xml.Unmarshal(data, &Gpx)
	if xmlerr != nil {
		panic(xmlerr)
	}

	for index, coord := range Gpx.Trk.Trkseg.Trkpt {
		fmt.Printf("[index : %d] lat : %s, lon: %s", index, coord.Lat, coord.Lon)
	}
}

์ƒ˜ํ”Œ XML ํŒŒ์ผ์˜ ํƒœ๊ทธ๊ตฌ์กฐ๊ฐ€ ์ชผ๊ธˆ ๋ณต์žกํ•˜์—ฌ Type ์ •์˜๊ฐ€ ์ข€ ๋ณต์žกํ•ด๋ณด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

์ตœ์ƒ๋‹จ์˜ Tag๋ถ€ํ„ฐ ์ตœํ•˜์œ„ ๋ ˆ๋ฒจ์˜ ํƒœ๊ทธ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ Type ์ •์˜ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

Type ์ •์˜ ํ›„์— ํŒŒ์ผ์„ ์ฝ๊ณ , ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ํŠน์ • ํƒœ๊ทธ๋ฅผ printํ•˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

 

 

[main.go]

package main

import (
	"go_parser/utils"
)

func main() {
	utils.Run()
}

main ํŒŒ์ผ์€ parser.go ํŒŒ์ผ์—์„œ ์ •์˜ํ•œ Run ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ๋‹ค์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  Golang์˜ ๋‚ด์šฉ์„ ๊ณต๋ถ€ํ•˜์‹œ๋ฉด ์•„์‹œ๊ฒ ์ง€๋งŒ

importํ•œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜์˜ ๋งจ ์•ž ๋ฌธ์ž๋Š” ๋ฐ˜๋“œ์‹œ ๋Œ€๋ฌธ์ž๋กœ ์ž‘์„ฑํ•˜์‹œ๋Š” ๊ฒƒ์€ ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค.

 

 

[์†Œ์Šค ์ „๋ฌธ]

github.com/JooHyeongLee/go_parser

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ