我有這個游樂場: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)
}
uj5u.com熱心網友回復:
基于@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。
uj5u.com熱心網友回復:
您代碼中的 xml 格式不正確。
xml 片段包含一個元素,該元素url 在其查詢字串部分包含一個帶有多個引數的 url,由 & 分隔&。
該符號在 xml 中具有特殊語意,用于啟動物體參考(一種符號常量) - 您不能單獨使用它。
要么將 url 寫為 CDATA 部分(其內容被認為是文字,語法:<![CDATA[... ]]>),要么替換所有出現的&with &(使用運動和符號的物體參考;在這里有效地用作轉義機制)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471896.html
下一篇:XSLT比較串列和輸出單節點
