(encoder).EncodeElement が「,innerxml」タグを無視するのはなぜですか?
php小编小新在这里为大家解答一个常见问题:“为什么 (encoder).EncodeElement 忽略“,innerxml”标签?”。这个问题涉及到在使用 (encoder).EncodeElement 方法时,为什么会出现无法编码 innerxml 标签的情况。下面我们将详细回答这个问题,帮助读者更好地理解和解决相关问题。
问题内容
用途:我有一个 xml 文档,其中包含许多混合内容 cdata 元素,我需要以编程方式编辑这些元素。令人烦恼的是,由于 cdata 元素具有其他/混合内容,默认的“,cdata”标记无法正常工作(根据 xml 规范)。如果您对此具体细节有疑问,请告诉我。
问题:在下面的简化示例中,我将其中包含 cdata 的元素标记为“,innerxml”,以便自己处理前缀/后缀。通过解组,一切都按预期工作,但是通过编组(编码),特殊字符被转义。当标签明确表示不转义时(通过“,innerxml”标签),为什么 EncodeElement 方法会转义特殊字符?当我在文档中读到此方法时,它让我参考 xml.Marshal 方法,其中包含以下内容:
<code> a field with tag ",innerxml" is written verbatim, not subject to the usual marshaling procedure. </code>
示例:
以下是代码(也可在 https://go.dev/play/p/MH_ONAVaG_1 获取):
package main import ( "encoding/xml" "fmt" "strings" ) var xmlFile string = `<?xml version="1.0" encoding="UTF-8"?> <statusdb> <status date="today"> <![CDATA[today is < yesterday]]> </status> <status date="yesterday"> <![CDATA[PM, 1. there are issues with the marshaller 2. i don't know how to solve them]]> </status> </statusdb>` type statusDB struct { Status []*status `xml:"status"` } type status struct { Text string `xml:",innerxml"` Date string `xml:"date,attr"` } type statusMarshaller status func 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\n", 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, "<![CDATA[") temp.Text = strings.TrimSuffix(temp.Text, "]]>") *tagElement = status(temp) return nil } func (tagElement status) MarshalXML(d *xml.Encoder, se xml.StartElement) error { tagElement.Text = "<![CDATA[" + tagElement.Text + "]]>" temp, _ := xml.Marshal(statusMarshaller(tagElement)) return d.EncodeElement(temp, se) }
此代码返回以下内容:
In Go: "today is < yesterday" In Go: "PM, 1. there are issues with the marshaller 2. i don't know how to solve them" <statusDB> <status><statusMarshaller date="today"><![CDATA[today is < yesterday]]></statusMarshaller></status> <status><statusMarshaller date="yesterday"><![CDATA[PM,
 1. there are issues with the marshaller
 2. i don't know how to solve them]]></statusMarshaller></status> </statusDB> Program exited.
结论:有人可以解释一下为什么 xml 包会这样做,以及潜在的解决方法是什么?
谢谢!
解决方法
当然,如果包中的 cdata 允许混合元素,那就太好了,但现在我已经找到了解决方法,即上面的代码,进行了一些小更改,以便不在 marhshalXML 中的 statusMarshaller 类型上调用“marshal”功能。相反,我只将 tagElement 转换为 statusMarshaller 类型,然后对该元素进行编码。请参阅以下详细信息:
修订历史记录:
- 修改了 marshalXML 函数中的第二行以删除对 xml.marshal 的调用
- 修改了状态结构以包含 XMLName 成员,以便维护 XML 元素名称(在生成的 xml 元素中保留“status”而不是“statusMarshaller”
package main import ( "encoding/xml" "fmt" "strings" ) var xmlFile string = `<?xml version="1.0" encoding="UTF-8"?> <statusdb> <status date="today"> <![CDATA[today is < yesterday]]> </status> <status date="yesterday"> <![CDATA[PM, 1. there are issues with the marshaller 2. i don't know how to solve them]]> </status> </statusdb>` type statusDB struct { Status []*status `xml:"status"` } type status struct { XMLName xml.Name Text string `xml:",innerxml"` Date string `xml:"date,attr"` } type statusMarshaller status func 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\n", 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, "<![CDATA[") temp.Text = strings.TrimSuffix(temp.Text, "]]>") *tagElement = status(temp) return nil } func (tagElement status) MarshalXML(d *xml.Encoder, se xml.StartElement) error { tagElement.Text = "<![CDATA[" + tagElement.Text + "]]>" temp := statusMarshaller(tagElement) return d.EncodeElement(temp, se) }
以上が(encoder).EncodeElement が「,innerxml」タグを無視するのはなぜですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









OpenSSLは、安全な通信で広く使用されているオープンソースライブラリとして、暗号化アルゴリズム、キー、証明書管理機能を提供します。ただし、その歴史的バージョンにはいくつかの既知のセキュリティの脆弱性があり、その一部は非常に有害です。この記事では、Debian SystemsのOpenSSLの共通の脆弱性と対応測定に焦点を当てます。 Debianopensslの既知の脆弱性:OpenSSLは、次のようないくつかの深刻な脆弱性を経験しています。攻撃者は、この脆弱性を、暗号化キーなどを含む、サーバー上の不正な読み取りの敏感な情報に使用できます。

この記事では、プロファイリングの有効化、データの収集、CPUやメモリの問題などの一般的なボトルネックの識別など、GOパフォーマンスを分析するためにPPROFツールを使用する方法について説明します。

この記事では、GOでユニットテストを書くことで、ベストプラクティス、モッキングテクニック、効率的なテスト管理のためのツールについて説明します。

Go Crawler Collyのキュースレッドの問題は、Go言語でColly Crawler Libraryを使用する問題を調査します。 �...

この記事では、go.modを介してGOモジュールの依存関係の管理、仕様、更新、競合解決をカバーすることについて説明します。セマンティックバージョンや定期的な更新などのベストプラクティスを強調しています。

バックエンド学習パス:フロントエンドからバックエンドへの探査の旅は、フロントエンド開発から変わるバックエンド初心者として、すでにNodeJSの基盤を持っています...

この記事では、GOでテーブル駆動型のテストを使用して説明します。これは、テストのテーブルを使用して複数の入力と結果を持つ関数をテストする方法です。読みやすさの向上、重複の減少、スケーラビリティ、一貫性、および
