为什么 (encoder).EncodeElement 忽略“,innerxml”标签?

为什么 (encoder).EncodeElement 忽略“,innerxml”标签?

php小编小新在这里为大家解答一个常见问题:“为什么 (encoder).EncodeElement 忽略“,innerxml”标签?”。这个问题涉及到在使用 (encoder).EncodeElement 方法时,为什么会出现无法编码 innerxml 标签的情况。下面我们将详细回答这个问题,帮助读者更好地理解和解决相关问题。

问题内容

用途:我有一个 xml 文档,其中包含许多混合内容 cdata 元素,我需要以编程方式编辑这些元素。令人烦恼的是,由于 cdata 元素具有其他/混合内容,默认的“,cdata”标记无法正常工作(根据 xml 规范)。如果您对此具体细节有疑问,请告诉我。

问题:在下面的简化示例中,我将其中包含 cdata 的元素标记为“,innerxml”,以便自己处理前缀/后缀。通过解组,一切都按预期工作,但是通过编组(编码),特殊字符被转义。当标签明确表示不转义时(通过“,innerxml”标签),为什么 EncodeElement 方法会转义特殊字符?当我在文档中读到此方法时,它让我参考 xml.Marshal 方法,其中包含以下内容:

  1. a field with tag ",innerxml" is written verbatim, not subject to the usual marshaling procedure.

登录后复制

示例:

以下是代码(也可在 https://go.dev/play/p/MH_ONAVaG_1 获取):

  1. package mainimport ( "encoding/xml" "fmt" "strings")var xmlFile string = ``type statusDB struct { Status []*status `xml:"status"`}type status struct { Text string `xml:",innerxml"` Date string `xml:"date,attr"`}type statusMarshaller statusfunc main() { var projectStatus statusDB err := xml.Unmarshal([]byte(xmlFile), &projectStatus) if err != nil { fmt.Println(err) return } fmt.Println("In Go: "" + projectStatus.Status[0].Text + """) fmt.Println("In Go: "" + projectStatus.Status[1].Text + """) x, err := xml.MarshalIndent(projectStatus, "", " ") if err != nil { fmt.Println(err) return } //why this is not printing properly fmt.Printf("%s", x)}func (tagElement *status) UnmarshalXML(d *xml.Decoder, se xml.StartElement) error { temp := statusMarshaller{} d.DecodeElement(&temp, &se) temp.Text = strings.TrimSpace(temp.Text) temp.Text = strings.TrimPrefix(temp.Text, "") *tagElement = status(temp) return nil}func (tagElement status) MarshalXML(d *xml.Encoder, se xml.StartElement) error { tagElement.Text = "" temp, _ := xml.Marshal(statusMarshaller(tagElement)) return d.EncodeElement(temp, se)}

登录后复制

此代码返回以下内容:

  1. In Go: "today is Program exited.

登录后复制

结论:有人可以解释一下为什么 xml 包会这样做,以及潜在的解决方法是什么?

谢谢!

解决方法

当然,如果包中的 cdata 允许混合元素,那就太好了,但现在我已经找到了解决方法,即上面的代码,进行了一些小更改,以便不在 marhshalXML 中的 statusMarshaller 类型上调用“marshal”功能。相反,我只将 tagElement 转换为 statusMarshaller 类型,然后对该元素进行编码。请参阅以下详细信息:

修订历史记录:

修改了 marshalXML 函数中的第二行以删除对 xml.marshal 的调用修改了状态结构以包含 XMLName 成员,以便维护 XML 元素名称(在生成的 xml 元素中保留“status”而不是“statusMarshaller”

  1. package mainimport ( "encoding/xml" "fmt" "strings")var xmlFile string = ``type statusDB struct { Status []*status `xml:"status"`}type status struct { XMLName xml.Name Text string `xml:",innerxml"` Date string `xml:"date,attr"`}type statusMarshaller statusfunc main() { var projectStatus statusDB err := xml.Unmarshal([]byte(xmlFile), &projectStatus) if err != nil { fmt.Println(err) return } fmt.Println("In Go: "" + projectStatus.Status[0].Text + """) fmt.Println("In Go: "" + projectStatus.Status[1].Text + """) x, err := xml.MarshalIndent(projectStatus, "", " ") if err != nil { fmt.Println(err) return } //why this is not printing properly fmt.Printf("%s", x)}func (tagElement *status) UnmarshalXML(d *xml.Decoder, se xml.StartElement) error { temp := statusMarshaller{} d.DecodeElement(&temp, &se) temp.Text = strings.TrimSpace(temp.Text) temp.Text = strings.TrimPrefix(temp.Text, "") *tagElement = status(temp) return nil}func (tagElement status) MarshalXML(d *xml.Encoder, se xml.StartElement) error { tagElement.Text = "" temp := statusMarshaller(tagElement) return d.EncodeElement(temp, se)}

登录后复制

以上就是为什么 (encoder).EncodeElement 忽略“,innerxml”标签?的详细内容,更多请关注【创想鸟】其它相关文章!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
编程技术

无法从 GoLang 运行 Cadence 工作流程

2025-3-1 17:00:24

编程技术

无法推断函数构造函数的泛型参数中的类型

2025-3-1 17:00:39

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索