feat(common): 添加 XML 根注释解析功能
- 在 monster_refresh.go 中添加 xmls 结构体,用于解析 XML 根注释 - 在 monster_refresh_test.go 中添加 TestMMMs 函数,实现根注释的捕获和输出 - 优化了 XML 解析逻辑,能够正确处理注释内容
This commit is contained in:
@@ -4,10 +4,16 @@ import (
|
|||||||
"github.com/ECUST-XX/xml"
|
"github.com/ECUST-XX/xml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type xmls struct {
|
||||||
|
Text string `xml:",chardata"`
|
||||||
|
Version string `xml:"comment"`
|
||||||
|
|
||||||
|
SuperMaps SuperMaps `xml:"superMaps"`
|
||||||
|
}
|
||||||
type SuperMaps struct {
|
type SuperMaps struct {
|
||||||
XMLName xml.Name `xml:"superMaps"`
|
XMLName xml.Name `xml:"superMaps"`
|
||||||
//Text string `xml:",chardata"`
|
Text string `xml:",comment"`
|
||||||
Maps []Maps `xml:"maps"`
|
Maps []Maps `xml:"maps"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Maps struct {
|
type Maps struct {
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ func Test_main(t *testing.T) {
|
|||||||
// })
|
// })
|
||||||
tf, _ := xml.MarshalIndentShortForm(tt, " ", " ")
|
tf, _ := xml.MarshalIndentShortForm(tt, " ", " ")
|
||||||
fmt.Println(string(tf))
|
fmt.Println(string(tf))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main1() {
|
func main1() {
|
||||||
@@ -124,3 +125,35 @@ func main1() {
|
|||||||
// fmt.Printf("#%d %s\n", i, n.InnerText())
|
// 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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user