go 的 `encoding/xml` 包默认会将 xml 元素的子节点(如 html 标签)解析为纯文本并忽略嵌套结构,若需原样保留 html 片段(如 `` 标签),应使用 `xml:",innerxml"` 标签而非普通字段映射。
在 Go 中解析包含内嵌 HTML 的 XML 时,常见的误区是直接将 HTML 内容映射为 string 类型字段(如 MyResult stringxml:"text`),这会导致xml.Unmarshal自动展开并丢弃所有子元素标签,仅提取纯文本内容(即“扁平化”处理)。例如
正确做法是利用 encoding/xml 提供的特殊结构标签 ",innerxml",它能将目标 XML 元素的原始内部 XML 字节流(包括所有标签、属性和文本)完整捕获为字符串。关键在于:必须将该字段嵌套在匿名或具名结构体中,并通过结构体字段进行映射。
以下是修正后的完整示例:
package main
import (
"encoding/xml"
"fmt"
)
type ResultSlice struct {
MyText []Result `xml:"results>result"`
}
type Result struct {
Text struct {
HTML string `xml:",innerxml"` // ✅ 捕获 内全部原始 XML 内容
} `xml:"text"`
}
func main() {
s := `
This has styleThen some not-style
No style here
Again, no style
`
r := &ResultSlice{}
if err := xml.Unmarshal([]byte(s), r); err != nil {
fmt.Printf("Parse error: %v\n", err)
return
}
for i, res := range r.MyText {
fmt.Printf("Result %d: %q\n", i+1, res.Text.HTML)
// 输出示例:
// Result 1: "This has styleThen some not-style"
// Result 2: "No style here"
// Result 3: "Again, no style"
}
} ⚠️ 注意事项:
总结:",innerxml" 是 Go XML 解析器中保留原始子节点序列的唯一可靠方式,合理封装结构体字段即可精准捕获含标签的 HTML 内容,避免信息丢失。