xml 字符编码互转
This commit is contained in:
parent
e540b8f964
commit
57e8c460b3
14
cmd/client/main.go
Normal file
14
cmd/client/main.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ping := netutil.IsPingConnected("127.0.0.1")
|
||||||
|
fmt.Println(ping)
|
||||||
|
|
||||||
|
ping = netutil.IsPingConnected("10.10.10.20")
|
||||||
|
fmt.Println(ping)
|
||||||
|
}
|
6
go.mod
6
go.mod
@ -3,13 +3,15 @@ module git.skcks.cn/Shikong/go-gb28181
|
|||||||
go 1.23.5
|
go 1.23.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
|
||||||
|
github.com/duke-git/lancet/v2 v2.3.4
|
||||||
github.com/emiago/sipgo v0.28.0
|
github.com/emiago/sipgo v0.28.0
|
||||||
github.com/icholy/digest v1.1.0
|
github.com/icholy/digest v1.1.0
|
||||||
golang.org/x/net v0.34.0
|
golang.org/x/net v0.34.0
|
||||||
|
golang.org/x/text v0.21.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
|
|
||||||
github.com/gobwas/httphead v0.1.0 // indirect
|
github.com/gobwas/httphead v0.1.0 // indirect
|
||||||
github.com/gobwas/pool v0.2.1 // indirect
|
github.com/gobwas/pool v0.2.1 // indirect
|
||||||
github.com/gobwas/ws v1.4.0 // indirect
|
github.com/gobwas/ws v1.4.0 // indirect
|
||||||
@ -19,6 +21,6 @@ require (
|
|||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/rs/zerolog v1.33.0 // indirect
|
github.com/rs/zerolog v1.33.0 // indirect
|
||||||
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b // indirect
|
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b // indirect
|
||||||
|
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a // indirect
|
||||||
golang.org/x/sys v0.29.0 // indirect
|
golang.org/x/sys v0.29.0 // indirect
|
||||||
golang.org/x/text v0.21.0 // indirect
|
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -4,6 +4,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
|
|||||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/duke-git/lancet/v2 v2.3.4 h1:8XGI7P9w+/GqmEBEXYaH/XuNiM0f4/90Ioti0IvYJls=
|
||||||
|
github.com/duke-git/lancet/v2 v2.3.4/go.mod h1:zGa2R4xswg6EG9I6WnyubDbFO/+A/RROxIbXcwryTsc=
|
||||||
github.com/emiago/sipgo v0.28.0 h1:wW9W+p89ldLbKOhXhj9BHo6U39vuWdUc0WVZ+E3rk6Y=
|
github.com/emiago/sipgo v0.28.0 h1:wW9W+p89ldLbKOhXhj9BHo6U39vuWdUc0WVZ+E3rk6Y=
|
||||||
github.com/emiago/sipgo v0.28.0/go.mod h1:ZQ/tl5t+3assyOjiKw/AInPkcawBJ2Or+d5buztOZsc=
|
github.com/emiago/sipgo v0.28.0/go.mod h1:ZQ/tl5t+3assyOjiKw/AInPkcawBJ2Or+d5buztOZsc=
|
||||||
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
|
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
|
||||||
@ -42,6 +44,8 @@ github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b h1:gQZ0qzfKHQIybL
|
|||||||
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a h1:4iLhBPcpqFmylhnkbY3W0ONLUYYkDAW9xMFLfxgsvCw=
|
||||||
|
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
||||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
85
pkg/utils/xml.go
Normal file
85
pkg/utils/xml.go
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"github.com/axgle/mahonia"
|
||||||
|
"golang.org/x/net/html/charset"
|
||||||
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
|
"golang.org/x/text/transform"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func XMLMarshalString(v interface{}, charset string) (string, error) {
|
||||||
|
marshal, err := XMLMarshal(v, charset)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(marshal), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func XMLMarshal(obj interface{}, charset string) ([]byte, error) {
|
||||||
|
marshal := &bytes.Buffer{}
|
||||||
|
encoder := xml.NewEncoder(marshal)
|
||||||
|
encoder.Indent("", " ")
|
||||||
|
err := encoder.Encode(&obj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = encoder.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
xmlStr := marshal.String()
|
||||||
|
cs := strings.ToUpper(charset)
|
||||||
|
xmlStr = fmt.Sprintf("<?xml version=\"1.0\" encoding=\"%s\" ?>\r\n%s", cs, xmlStr)
|
||||||
|
|
||||||
|
xmlBytes := &bytes.Buffer{}
|
||||||
|
err = func() (err error) {
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
var t transform.Transformer
|
||||||
|
switch strings.ToUpper(charset) {
|
||||||
|
case "GBK":
|
||||||
|
t = simplifiedchinese.GBK.NewEncoder()
|
||||||
|
case "GB2312":
|
||||||
|
t = simplifiedchinese.HZGB2312.NewEncoder()
|
||||||
|
case "GB18030":
|
||||||
|
t = simplifiedchinese.GB18030.NewEncoder()
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf("unsupported charset: %s", charset)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writer := transform.NewWriter(xmlBytes, t)
|
||||||
|
writer.Write([]byte(xmlStr))
|
||||||
|
_ = writer.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
enc := mahonia.NewEncoder(charset)
|
||||||
|
xmlStr := enc.ConvertString(xmlStr)
|
||||||
|
xmlBytes.Write([]byte(xmlStr))
|
||||||
|
return nil
|
||||||
|
}()
|
||||||
|
|
||||||
|
return xmlBytes.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func XMLUnmarshal(data []byte, obj interface{}) error {
|
||||||
|
decoder := xml.NewDecoder(bytes.NewReader(data))
|
||||||
|
decoder.CharsetReader = func(c string, input io.Reader) (io.Reader, error) {
|
||||||
|
reader, err := charset.NewReader(input, c)
|
||||||
|
return reader, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err := decoder.Decode(&obj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -191,3 +191,18 @@ func TestGB2312XML(t *testing.T) {
|
|||||||
decoder.Decode(&obj)
|
decoder.Decode(&obj)
|
||||||
fmt.Printf("%+v\n", obj)
|
fmt.Printf("%+v\n", obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestXML(t *testing.T) {
|
||||||
|
fmt.Printf("%s\n", GB2312_XML)
|
||||||
|
|
||||||
|
obj := Catalog{}
|
||||||
|
err := XMLUnmarshal([]byte(GB2312_XML), &obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("%+v\n", &obj)
|
||||||
|
|
||||||
|
fmt.Println(XMLMarshalString(&obj, "GB2312"))
|
||||||
|
fmt.Println(XMLMarshalString(&obj, "GBK"))
|
||||||
|
fmt.Println(XMLMarshalString(&obj, "UTF8"))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user