猿问

无法在 GO 中使用以下标签解析 xml:在标签中

我发现如果 XML 文件中的标签包含:在 Go 中的解组代码似乎不起作用。任何见解?


例如,在下面的 XML 文件中,Summary可以运行,但不能运行Cevent.


<summary>...AIR QUALITY ALERT </summary>

<cap:event>Air Quality Alert</cap:event>

type Entry struct{

    Summary    string   `xml:"summary"`

    Cevent     string   `xml:"cap:event"`

}


繁华开满天机
浏览 198回答 3
3回答

紫衣仙女

cap是命名空间标识符,而不是标记名称的一部分。这里是简写urn:oasis:names:tc:emergency:cap:1.1(这个答案看起来可能对命名空间有一个很好的浓缩解释:XML 中的“xmlns”是什么意思?)Go“encoding/xml”包不能很好地处理命名空间,但如果没有冲突的标签,你可以完全省略命名空间type Entry struct {&nbsp; &nbsp; Summary string `xml:"summary"`&nbsp; &nbsp; Event&nbsp; &nbsp;string `xml:"event"`}指定事件的正确方法,尤其是在不同命名空间中具有相同标签的情况下,将使用完整命名空间,例如:type Entry struct {&nbsp; &nbsp; Summary string `xml:"summary"`&nbsp; &nbsp; Event&nbsp; &nbsp;string `xml:"urn:oasis:names:tc:emergency:cap:1.1 event"`}这是一个工作示例:https : //play.golang.org/p/ry55F2pWKY

天涯尽头无女友

cap不是标记名称的一部分,而是命名空间标识符(urn:oasis:names:tc:emergency:cap:1.1如您在评论中提供的那样的缩写)。这是正确的符号:type Entry struct{&nbsp; &nbsp; Summary&nbsp; &nbsp; string&nbsp; &nbsp;`xml:"summary"`&nbsp; &nbsp; Cevent&nbsp; &nbsp; &nbsp;string&nbsp; &nbsp;`xml:"urn:oasis:names:tc:emergency:cap:1.1:cap event"`}注意空格而不是:表示命名空间。另请注意,仅使用名称空间标识符(如xml:"cap event")是行不通的。工作示例(https://play.golang.org/p/rjkb2esGgv):package mainimport "fmt"import "encoding/xml"type Entry struct{&nbsp; &nbsp; Summary&nbsp; &nbsp; string&nbsp; &nbsp;`xml:"summary"`&nbsp; &nbsp; Cevent&nbsp; &nbsp; &nbsp;string&nbsp; &nbsp;`xml:"urn:oasis:names:tc:emergency:cap:1.1:cap event"`}func main() {&nbsp; &nbsp; xmlString := []byte(`&nbsp; &nbsp; &nbsp; &nbsp; <doc xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <summary>...AIR QUALITY ALERT </summary>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <cap:event>Air Quality Alert</cap:event>&nbsp; &nbsp; &nbsp; &nbsp; </doc>&nbsp; &nbsp; `)&nbsp; &nbsp; entry := new(Entry)&nbsp; &nbsp; if err := xml.Unmarshal(xmlString, entry); err == nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(entry)&nbsp; &nbsp; }&nbsp;}

12345678_0001

你只需要逃避冒号。因此,将您的 xml 标记更改为xml:"cap\:event",它将按您的预期工作。type Entry struct{&nbsp; &nbsp; Summary&nbsp; &nbsp; string&nbsp; &nbsp;`xml:"summary"`&nbsp; &nbsp; Cevent&nbsp; &nbsp; &nbsp;string&nbsp; &nbsp;`xml:"cap\:event"`}使用 unmarshal 示例在 xml 页面上对此进行了测试,并稍作修改;package mainimport (&nbsp; &nbsp; "encoding/xml"&nbsp; &nbsp; "fmt")func main() {&nbsp; &nbsp; type Email struct {&nbsp; &nbsp; &nbsp; &nbsp; Where string `xml:"where,attr"`&nbsp; &nbsp; &nbsp; &nbsp; Addr&nbsp; string&nbsp; &nbsp; }&nbsp; &nbsp; type Address struct {&nbsp; &nbsp; &nbsp; &nbsp; City, State string&nbsp; &nbsp; }&nbsp; &nbsp; type Result struct {&nbsp; &nbsp; &nbsp; &nbsp; XMLName xml.Name `xml:"Person"`&nbsp; &nbsp; &nbsp; &nbsp; Name&nbsp; &nbsp; string&nbsp; &nbsp;`xml:"Full\:Name"`&nbsp; &nbsp; &nbsp; &nbsp; Phone&nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; Email&nbsp; &nbsp;[]Email&nbsp; &nbsp; &nbsp; &nbsp; Groups&nbsp; []string `xml:"Group>Value"`&nbsp; &nbsp; &nbsp; &nbsp; Address&nbsp; &nbsp; }&nbsp; &nbsp; v := Result{Name: "none", Phone: "none"}&nbsp; &nbsp; data := `&nbsp; &nbsp; &nbsp; &nbsp; <Person>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Full:Name>Grace R. Emlin</Full:Name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Company>Example Inc.</Company>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Email where="home">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Addr>gre@example.com</Addr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Email>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Email where='work'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Addr>gre@work.com</Addr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Email>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Group>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Value>Friends</Value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Value>Squash</Value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Group>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <City>Hanga Roa</City>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <State>Easter Island</State>&nbsp; &nbsp; &nbsp; &nbsp; </Person>&nbsp; &nbsp; `&nbsp; &nbsp; err := xml.Unmarshal([]byte(data), &v)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("error: %v", err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("XMLName: %#v\n", v.XMLName)&nbsp; &nbsp; fmt.Printf("Name: %q\n", v.Name)&nbsp; &nbsp; fmt.Printf("Phone: %q\n", v.Phone)&nbsp; &nbsp; fmt.Printf("Email: %v\n", v.Email)&nbsp; &nbsp; fmt.Printf("Groups: %v\n", v.Groups)&nbsp; &nbsp; fmt.Printf("Address: %v\n", v.Address)}删除转义符,它将为 Name 打印“none”。使用空格代替:or \:,它也可以工作。xml 中的空格会导致解析错误,因为它显然是无效的。
随时随地看视频慕课网APP

相关分类

Go
我要回答