From e04b8591378c462bdfd4bbd711e10c00f33e708d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=94=E5=BF=B5?= <1@72wo.cn> Date: Fri, 4 Jul 2025 18:53:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(common):=20=E6=B7=BB=E5=8A=A0=20XML=20?= =?UTF-8?q?=E6=A0=B9=E6=B3=A8=E9=87=8A=E8=A7=A3=E6=9E=90=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 monster_refresh.go 中添加 xmls 结构体,用于解析 XML 根注释 - 在 monster_refresh_test.go 中添加 TestMMMs 函数,实现根注释的捕获和输出 - 优化了 XML 解析逻辑,能够正确处理注释内容 --- common/data/xml/monster_refresh.go | 10 ++++++-- common/data/xml/monster_refresh_test.go | 33 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/common/data/xml/monster_refresh.go b/common/data/xml/monster_refresh.go index 83edde558..76add1607 100644 --- a/common/data/xml/monster_refresh.go +++ b/common/data/xml/monster_refresh.go @@ -4,10 +4,16 @@ import ( "github.com/ECUST-XX/xml" ) +type xmls struct { + Text string `xml:",chardata"` + Version string `xml:"comment"` + + SuperMaps SuperMaps `xml:"superMaps"` +} type SuperMaps struct { XMLName xml.Name `xml:"superMaps"` - //Text string `xml:",chardata"` - Maps []Maps `xml:"maps"` + Text string `xml:",comment"` + Maps []Maps `xml:"maps"` } type Maps struct { diff --git a/common/data/xml/monster_refresh_test.go b/common/data/xml/monster_refresh_test.go index 42e975230..46387aec0 100644 --- a/common/data/xml/monster_refresh_test.go +++ b/common/data/xml/monster_refresh_test.go @@ -67,6 +67,7 @@ func Test_main(t *testing.T) { // }) tf, _ := xml.MarshalIndentShortForm(tt, " ", " ") fmt.Println(string(tf)) + } func main1() { @@ -124,3 +125,35 @@ func main1() { // fmt.Printf("#%d %s\n", i, n.InnerText()) // } } + +func TestMMMs(t *testing.T) { + + decoder := xml.NewDecoder(strings.NewReader(s)) + var rootComments []string + rootFound := false + + for { + token, err := decoder.Token() + if err != nil { + break + } + + switch t := token.(type) { + case xml.StartElement: + // 遇到根元素开始标签后停止收集注释 + rootFound = true + fmt.Printf("找到根元素: %s\n", t.Name.Local) + case xml.Comment: + if !rootFound { + // 保存根元素前的注释 + rootComments = append(rootComments, string(t)) + } + } + } + + // 输出捕获的根注释 + fmt.Println("\n根注释内容:") + for _, comment := range rootComments { + fmt.Println(strings.TrimSpace(comment)) + } +}