英文:
What is invalid character entity &ccb
问题
我有这个游乐场:https://go.dev/play/p/uEpYEWaQaV0
但是我不知道问题出在哪里,为什么它不工作!我得到了以下错误:
invalid character entity &ccb
我的代码是:
package main
import (
"encoding/xml"
"fmt"
)
type catalog struct {
Id string `xml:"id"`
}
type price struct {
Subtotal int `xml:"subtotal"`
Currency string `xml:"currency"`
Total int `xml:"total"`
PriceStatus string `xml:"price_status"`
}
type image struct {
Url string `xml:"url"`
Id int `xml:"id"`
}
type product struct {
Id int `xml:"id"`
RetailerId int `xml:"retailer_id"`
Image image `xml:"image"`
Price int `xml:"price"`
Currency string `xml:"currency"`
Name string `xml:"name"`
Quantity int `xml:"quantity"`
}
type order struct {
Product product `xml:"product"`
Catalog catalog `xml:"catalog"`
Price price `xml:"price"`
}
type Iq struct {
XMLName xml.Name `xml:"iq"`
Order order `xml:"order"`
}
func main() {
contents := `<iq from="s.whatsapp.net" id="162.120-3" type="result">
<order creation_ts="1651703902" id="1046755402590219">
<product>
<id>8312993582051445</id>
<retailer_id>291</retailer_id>
<image>
<url>https://mmg.whatsapp.net/v/t45.5328-4/279646282_7510595942346471_4295336878174066544_n.jpg?stp=dst-jpg_p100x100&ccb=1-5&_nc_sid=c48759&_nc_ohc=OMyHhkGxzRoAX8Dn93Q&_nc_ad=z-m&_nc_cid=0&_nc_ht=mmg.whatsapp.net&oh=01_AVw_0loIIuK1LP-n5OL1hdpRmNYhAiUjLGk20FCclgNXCA&oe=62774C93</url>
<id>7510595939013138</id>
</image>
<price>5000</price>
<currency>SAR</currency>
<name>coffee</name>
<quantity>1</quantity>
</product>
<catalog><id>326185462964376</id></catalog>
<price>
<subtotal>5000</subtotal>
<currency>SAR</currency>
<total>5000</total>
<price_status>provided</price_status>
</price>
</order>
</iq>`
iq := &Iq{}
err := xml.Unmarshal([]byte(contents), &iq)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", iq.Order)
}
英文:
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:
invalid character entity &ccb
My code is:
package main
import (
"encoding/xml"
"fmt"
)
type catalog struct {
Id string `xml:"id"`
}
type price struct {
Subtotal int `xml:"subtotal"`
Currency string `xml:"currency"`
Total int `xml:"total"`
PriceStatus string `xml:"price_status"`
}
type image struct {
Url string `xml:"url"`
Id int `xml:"id"`
}
type product struct {
Id int `xml:"id"`
RetailerId int `xml:"retailer_id"`
Image image `xml:"image"`
Price int `xml:"price"`
Currency string `xml:"currency"`
Name string `xml:"name"`
Quantity int `xml:"quantity"`
}
type order struct {
Product product `xml:"product"`
Catalog catalog `xml:"catalog"`
Price price `xml:"price"`
}
type Iq struct {
XMLName xml.Name `xml:"iq"`
Order order `xml:"order"`
}
func main() {
contents := `<iq from="s.whatsapp.net" id="162.120-3" type="result">
<order creation_ts="1651703902" id="1046755402590219">
<product>
<id>8312993582051445</id>
<retailer_id>291</retailer_id>
<image>
<url>https://mmg.whatsapp.net/v/t45.5328-4/279646282_7510595942346471_4295336878174066544_n.jpg?stp=dst-jpg_p100x100&ccb=1-5&_nc_sid=c48759&_nc_ohc=OMyHhkGxzRoAX8Dn93Q&_nc_ad=z-m&_nc_cid=0&_nc_ht=mmg.whatsapp.net&oh=01_AVw_0loIIuK1LP-n5OL1hdpRmNYhAiUjLGk20FCclgNXCA&oe=62774C93</url>
<id>7510595939013138</id>
</image>
<price>5000</price>
<currency>SAR</currency>
<name>coffee</name>
<quantity>1</quantity>
</product>
<catalog><id>326185462964376</id></catalog>
<price>
<subtotal>5000</subtotal>
<currency>SAR</currency>
<total>5000</total>
<price_status>provided</price_status>
</price>
</order>
</iq>`
iq := &Iq{}
err := xml.Unmarshal([]byte(contents), &iq)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", iq.Order)
}
答案1
得分: 5
基于@collapsar的答案...
尽管XML格式不正确,但您仍然可以通过创建一个Decoder
实例并关闭Strict
模式来处理它:
d := xml.NewDecoder(bytes.NewReader([]byte(contents)))
d.Strict = false
err := d.Decode(&iq)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", iq.Order)
有关参考,请参阅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:
d := xml.NewDecoder(bytes.NewReader([]byte(contents)))
d.Strict = false
err := d.Decode(&iq)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", iq.Order)
For reference, see https://pkg.go.dev/encoding/xml#Decoder.
答案2
得分: 3
你的代码中的 XML 不是格式良好的。
XML 片段包含一个名为 url
的元素,其中包含一个带有多个参数的 URL,这些参数在查询字符串部分由和号 &
分隔。
在 XML 中,这个符号具有特殊的语义,用于引用实体(一种符号常量)- 你不能单独使用它。
要么将 URL 写成 CDATA 部分(其中的内容被视为字面值,语法:<![CDATA[
...]]>
),要么将所有的 &
替换为 &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 &
.
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: <![CDATA[
...]]>
) or replace all occurrences of &
with &amp;
(using the entity reference for the sports and symbol; effectively used here as an escaping mechanism).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论