What is invalid character entity &ccb

huangapple go评论121阅读模式
英文:

What is invalid character entity &ccb

问题

我有这个游乐场:https://go.dev/play/p/uEpYEWaQaV0
但是我不知道问题出在哪里,为什么它不工作!我得到了以下错误:

  1. invalid character entity &ccb

我的代码是:

  1. package main
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. )
  6. type catalog struct {
  7. Id string `xml:"id"`
  8. }
  9. type price struct {
  10. Subtotal int `xml:"subtotal"`
  11. Currency string `xml:"currency"`
  12. Total int `xml:"total"`
  13. PriceStatus string `xml:"price_status"`
  14. }
  15. type image struct {
  16. Url string `xml:"url"`
  17. Id int `xml:"id"`
  18. }
  19. type product struct {
  20. Id int `xml:"id"`
  21. RetailerId int `xml:"retailer_id"`
  22. Image image `xml:"image"`
  23. Price int `xml:"price"`
  24. Currency string `xml:"currency"`
  25. Name string `xml:"name"`
  26. Quantity int `xml:"quantity"`
  27. }
  28. type order struct {
  29. Product product `xml:"product"`
  30. Catalog catalog `xml:"catalog"`
  31. Price price `xml:"price"`
  32. }
  33. type Iq struct {
  34. XMLName xml.Name `xml:"iq"`
  35. Order order `xml:"order"`
  36. }
  37. func main() {
  38. contents := `<iq from="s.whatsapp.net" id="162.120-3" type="result">
  39. <order creation_ts="1651703902" id="1046755402590219">
  40. <product>
  41. <id>8312993582051445</id>
  42. <retailer_id>291</retailer_id>
  43. <image>
  44. <url>https://mmg.whatsapp.net/v/t45.5328-4/279646282_7510595942346471_4295336878174066544_n.jpg?stp=dst-jpg_p100x100&amp;ccb=1-5&amp;_nc_sid=c48759&amp;_nc_ohc=OMyHhkGxzRoAX8Dn93Q&amp;_nc_ad=z-m&amp;_nc_cid=0&amp;_nc_ht=mmg.whatsapp.net&amp;oh=01_AVw_0loIIuK1LP-n5OL1hdpRmNYhAiUjLGk20FCclgNXCA&amp;oe=62774C93</url>
  45. <id>7510595939013138</id>
  46. </image>
  47. <price>5000</price>
  48. <currency>SAR</currency>
  49. <name>coffee</name>
  50. <quantity>1</quantity>
  51. </product>
  52. <catalog><id>326185462964376</id></catalog>
  53. <price>
  54. <subtotal>5000</subtotal>
  55. <currency>SAR</currency>
  56. <total>5000</total>
  57. <price_status>provided</price_status>
  58. </price>
  59. </order>
  60. </iq>`
  61. iq := &Iq{}
  62. err := xml.Unmarshal([]byte(contents), &iq)
  63. if err != nil {
  64. panic(err)
  65. }
  66. fmt.Printf("%v\n", iq.Order)
  67. }
英文:

I've this playground: https://go.dev/play/p/uEpYEWaQaV0
But did not get what is the issue and why it is not working! and I'm getting the error:

  1. invalid character entity &amp;ccb

My code is:

  1. package main
  2. import (
  3. &quot;encoding/xml&quot;
  4. &quot;fmt&quot;
  5. )
  6. type catalog struct {
  7. Id string `xml:&quot;id&quot;`
  8. }
  9. type price struct {
  10. Subtotal int `xml:&quot;subtotal&quot;`
  11. Currency string `xml:&quot;currency&quot;`
  12. Total int `xml:&quot;total&quot;`
  13. PriceStatus string `xml:&quot;price_status&quot;`
  14. }
  15. type image struct {
  16. Url string `xml:&quot;url&quot;`
  17. Id int `xml:&quot;id&quot;`
  18. }
  19. type product struct {
  20. Id int `xml:&quot;id&quot;`
  21. RetailerId int `xml:&quot;retailer_id&quot;`
  22. Image image `xml:&quot;image&quot;`
  23. Price int `xml:&quot;price&quot;`
  24. Currency string `xml:&quot;currency&quot;`
  25. Name string `xml:&quot;name&quot;`
  26. Quantity int `xml:&quot;quantity&quot;`
  27. }
  28. type order struct {
  29. Product product `xml:&quot;product&quot;`
  30. Catalog catalog `xml:&quot;catalog&quot;`
  31. Price price `xml:&quot;price&quot;`
  32. }
  33. type Iq struct {
  34. XMLName xml.Name `xml:&quot;iq&quot;`
  35. Order order `xml:&quot;order&quot;`
  36. }
  37. func main() {
  38. contents := `&lt;iq from=&quot;s.whatsapp.net&quot; id=&quot;162.120-3&quot; type=&quot;result&quot;&gt;
  39. &lt;order creation_ts=&quot;1651703902&quot; id=&quot;1046755402590219&quot;&gt;
  40. &lt;product&gt;
  41. &lt;id&gt;8312993582051445&lt;/id&gt;
  42. &lt;retailer_id&gt;291&lt;/retailer_id&gt;
  43. &lt;image&gt;
  44. &lt;url&gt;https://mmg.whatsapp.net/v/t45.5328-4/279646282_7510595942346471_4295336878174066544_n.jpg?stp=dst-jpg_p100x100&amp;ccb=1-5&amp;_nc_sid=c48759&amp;_nc_ohc=OMyHhkGxzRoAX8Dn93Q&amp;_nc_ad=z-m&amp;_nc_cid=0&amp;_nc_ht=mmg.whatsapp.net&amp;oh=01_AVw_0loIIuK1LP-n5OL1hdpRmNYhAiUjLGk20FCclgNXCA&amp;oe=62774C93&lt;/url&gt;
  45. &lt;id&gt;7510595939013138&lt;/id&gt;
  46. &lt;/image&gt;
  47. &lt;price&gt;5000&lt;/price&gt;
  48. &lt;currency&gt;SAR&lt;/currency&gt;
  49. &lt;name&gt;coffee&lt;/name&gt;
  50. &lt;quantity&gt;1&lt;/quantity&gt;
  51. &lt;/product&gt;
  52. &lt;catalog&gt;&lt;id&gt;326185462964376&lt;/id&gt;&lt;/catalog&gt;
  53. &lt;price&gt;
  54. &lt;subtotal&gt;5000&lt;/subtotal&gt;
  55. &lt;currency&gt;SAR&lt;/currency&gt;
  56. &lt;total&gt;5000&lt;/total&gt;
  57. &lt;price_status&gt;provided&lt;/price_status&gt;
  58. &lt;/price&gt;
  59. &lt;/order&gt;
  60. &lt;/iq&gt;`
  61. iq := &amp;Iq{}
  62. err := xml.Unmarshal([]byte(contents), &amp;iq)
  63. if err != nil {
  64. panic(err)
  65. }
  66. fmt.Printf(&quot;%v\n&quot;, iq.Order)
  67. }

答案1

得分: 5

基于@collapsar的答案...

尽管XML格式不正确,但您仍然可以通过创建一个Decoder实例并关闭Strict模式来处理它:

  1. d := xml.NewDecoder(bytes.NewReader([]byte(contents)))
  2. d.Strict = false
  3. err := d.Decode(&iq)
  4. if err != nil {
  5. panic(err)
  6. }
  7. fmt.Printf("%v\n", iq.Order)

Go Playground

有关参考,请参阅https://pkg.go.dev/encoding/xml#Decoder。

英文:

Building on the answer from @collapsar...

Despite the XML being malformed, you can still process it by creating a Decoder instance and turning off Strict mode:

  1. d := xml.NewDecoder(bytes.NewReader([]byte(contents)))
  2. d.Strict = false
  3. err := d.Decode(&amp;iq)
  4. if err != nil {
  5. panic(err)
  6. }
  7. fmt.Printf(&quot;%v\n&quot;, iq.Order)

Go Playground

For reference, see https://pkg.go.dev/encoding/xml#Decoder.

答案2

得分: 3

你的代码中的 XML 不是格式良好的。

XML 片段包含一个名为 url 的元素,其中包含一个带有多个参数的 URL,这些参数在查询字符串部分由和号 &amp; 分隔。

在 XML 中,这个符号具有特殊的语义,用于引用实体(一种符号常量)- 你不能单独使用它。

要么将 URL 写成 CDATA 部分(其中的内容被视为字面值,语法:&lt;![CDATA[...]]&gt;),要么将所有的 &amp; 替换为 &amp;amp;(使用实体引用来转义和号符号,在这里有效地用作转义机制)。

英文:

The xml in your code is not well-formed.

The xml fragment contains an element url which contains a url with multiple parameters in its query string portion, separated by ampersands &amp;.

This symbol has special semantics in xml in initiating an entity reference (kind of a symbolic constant) - you cannot use it in isolation.

Either write the url as a CDATA section (the contents of which are considered a literal, Syntax: &lt;![CDATA[...]]&gt;) or replace all occurrences of &amp; with &amp;amp; (using the entity reference for the sports and symbol; effectively used here as an escaping mechanism).

huangapple
  • 本文由 发表于 2022年5月5日 06:48:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/72120226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定