问题描述
我想解析一个 Xml 文件的属性.它适用于任何正常"属性,例如
I want to parse the attribute of an Xml file.It works for any "normal" attributes like for instance <application name="AppName">
但是如果属性中有:",我就无法检索它的值,例如 <application name:test="AppName">
But I can't manage to retrieve the value of the attribute if it has a ":" in it., like <application name:test="AppName">
这是我用来解析这个的代码:
Here is the code that I am using to parse this :
package main
import "fmt"
import "encoding/xml"
type Application struct {
Event Event `xml:"application"`
Package string `xml:"package,attr"`
}
type Event struct {
IsValid string `xml:"test:isValid,attr"`
}
var doc = []byte(`<?xml version="1.0" encoding="utf-8" standalone="no"?>
<application package="leNomDuPackage">
<event test:isValid="true">
</application>
</manifest>`)
func main() {
application := Application{}
xml.Unmarshal(doc, &application)
fmt.Println("Application: ", application)
fmt.Println("isValid:", application.Event)
}
你也可以在 Golang playground 上找到它:[https://play.golang.org/p/R6H80xPezhm
You can find it on the Golang playground as well : [https://play.golang.org/p/R6H80xPezhm
我想检索 isValid
属性的值.
I want to retrieve the value of the isValid
attribute.
目前,我收到该错误消息,但无法解决.
At the moment, I have that error message and I can't manage to solve it.
struct field tag xml:"test\:isValid,attr
与reflect.StructTag.Get 不兼容:struct 标记值的语法错误
我也尝试使用以下值
type Event struct {
IsValid string `xml:"test isValid,attr`
}
和
type Event struct {
IsValid string `xml:"test\:isValid,attr`
}
但效果不佳.
推荐答案
您可以省略标签定义中的 "test:"
前缀.只需确保您的 XML 是有效的,您的 XML 没有 的结束标记,并且有一个不匹配的结束标记
</manifest>
.您还缺少标记定义中的右引号.
You can leave out the "test:"
prefix in the tag definition. Just make sure your XML is valid, yours don't have a closing tag for <event>
and has an unmatched closing tag </manifest>
. You're also missing a closing quotation mark in the tag definition.
型号:
type Application struct {
Event Event `xml:"event"`
Package string `xml:"package,attr"`
}
type Event struct {
IsValid string `xml:"isValid,attr"`
}
一个有效的 XML 示例:
A valid XML example:
var doc = `
<application package="leNomDuPackage">
<event test:isValid="true" />
</application>`
代码解析:
application := Application{}
err := xml.Unmarshal([]byte(doc), &application)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Application: %#v\n", application)
输出(在 Go Playground 上试试):
Output (try it on the Go Playground):
Application: main.Application{Event:main.Event{IsValid:"true"},
Package:"leNomDuPackage"}
注意,如果你有多个同名但前缀不同的属性,比如这个例子:
Note that if you have multiple attributes with the same name but different prefixes, such as this example:
var doc = `
<application package="leNomDuPackage">
<event test:isValid="true" test2:isValid="false" />
</application>`
然后您可以在标签值中添加命名空间前缀,与名称之间用空格分隔,如下所示:
Then you may add the namespace prefix in the tag value, separated by a space from the name, like this:
type Event struct {
IsValid1 string `xml:"test isValid,attr"`
IsValid2 string `xml:"test2 isValid,attr"`
}
解析代码是一样的.输出(在 Go Playground 上试试):
Parsing code is the same. Output (try it on the Go Playground):
Application: main.Application{Event:main.Event{IsValid1:"true",
IsValid2:"false"}, Package:"leNomDuPackage"}
这篇关于解析 GO 中的 Xml 以获取带有 &quot;:&quot 的属性在标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!