From b9826a24a06895a41a3c29b9ee8c9b04ad03205d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 23 Oct 2024 18:41:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81xml=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- file.go | 25 ++++++++++++++++++++++--- xml.go | 4 ++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/file.go b/file.go index 4b4f2e8..f9366fa 100644 --- a/file.go +++ b/file.go @@ -8,6 +8,7 @@ package serialize import ( + "encoding/xml" "errors" "git.zhangdeman.cn/zhangdeman/consts" "io" @@ -22,8 +23,6 @@ import ( "git.zhangdeman.cn/zhangdeman/util/define" "github.com/go-ini/ini" - - yml "gopkg.in/yaml.v3" ) var ( @@ -78,7 +77,7 @@ func (f *file) ReadYmlContent(filePath string, result any) error { if fileContent, err = f.ReadFileContent(filePath); nil != err { return err } - return yml.Unmarshal(fileContent, result) + return Yml.UnmarshalWithNumber(fileContent, result) } // ReadJSONContent 读取JSON内容,并解析到指定的结构体中 @@ -127,6 +126,24 @@ func (f *file) ReadTomlContent(filePath string, result any) error { return nil } +// ReadXmlContent ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:39 2024/10/23 +func (f *file) ReadXmlContent(filePath string, result any) error { + var ( + fileContent []byte + err error + ) + + if fileContent, err = f.ReadFileContent(filePath); nil != err { + return err + } + + return xml.Unmarshal(fileContent, result) +} + // ReadAnyFileContent 读取任意类型的文件并解析 // // Author : go_developer@163.com<白茶清欢> @@ -153,6 +170,8 @@ func (f *file) ReadAnyFileContent(filePath string, receiver any) error { parseFunc = f.ReadIniContent case consts.FileTypeToml: parseFunc = f.ReadTomlContent + case consts.FileTypeXml: + parseFunc = f.ReadXmlContent default: return errors.New(fileExt + " 暂不支持当前格式的文件解析") } diff --git a/xml.go b/xml.go index 048ad52..f7d4250 100644 --- a/xml.go +++ b/xml.go @@ -13,6 +13,10 @@ import ( "io" ) +var ( + Xml = &ownXml{} +) + type ownXml struct{} func (o *ownXml) UnmarshalWithNumber(byteData []byte, receiver any) error {