From e209119dd57bd3befcbe266f0e77220d0ef5aa02 Mon Sep 17 00:00:00 2001 From: wenzi1 Date: Wed, 6 Jun 2018 14:12:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84gxml=E4=B8=AD=E5=8C=B9?= =?UTF-8?q?=E9=85=8Dencoding=E7=9A=84=E5=A4=84=E7=90=86=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0gcharset=E5=8C=85=E7=9A=84=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/encoding/gcharset/gcharset.go | 5 ++++- g/encoding/gxml/gxml.go | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/g/encoding/gcharset/gcharset.go b/g/encoding/gcharset/gcharset.go index b67858c8e..4a2490759 100644 --- a/g/encoding/gcharset/gcharset.go +++ b/g/encoding/gcharset/gcharset.go @@ -7,7 +7,8 @@ //gcharset //@author wenzi1 //@date 20180604 - +//字符集转换方法. +//使用mahonia实现的字符集转换方法,支持的字符集包括常见的utf8/UTF-16/UTF-16LE/macintosh/big5/gbk/gb18030,支持的全量字符集可以参考mahonia包 package gcharset import ( @@ -16,6 +17,8 @@ import ( "fmt" ) + + //获取支持的字符集 func GetCharset(name string) (*mahonia.Charset, error) { s := mahonia.GetCharset(name) diff --git a/g/encoding/gxml/gxml.go b/g/encoding/gxml/gxml.go index 29ac497ab..ac16eb4dc 100644 --- a/g/encoding/gxml/gxml.go +++ b/g/encoding/gxml/gxml.go @@ -11,7 +11,7 @@ import ( "github.com/clbanning/mxj" "encoding/xml" "io" - "gitee.com/johng/gf/g/encoding/gcharset" + "gitee.com/wenzi1/gf/g/encoding/gcharset" "gitee.com/johng/gf/g/util/gregx" ) @@ -45,7 +45,7 @@ func ToJson(xmlbyte []byte) ([]byte, error) { //@author wenzi1 //@date 20180604 func Prepare(xmlbyte []byte) error { - patten := "<\\?xml\\s+.*?\\s+encoding=\"(.*?)\"" + patten := "<\\?xml\\s+version\\s*=.*?\\s+encoding\\s*=\\s*[\\'|\"](.*?)[\\'|\"]\\s*\\?\\s*>" charsetReader := func(charset string, input io.Reader) (io.Reader, error) { reader, err := gcharset.GetCharset(charset) if err != nil {