解析GO中的Xml以归因于“:"在标签中 [英] Parse Xml in GO for atttribute with ":" in tag
问题描述
我想解析Xml文件的属性.它适用于任何普通"属性,例如< application name ="AppName">
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游乐场找到它:[ https://play.golang.org/p/R6H80xPezhm
You can find it on the Golang playground as well : [https://play.golang.org/p/R6H80xPezhm
我想检索 isValid
属性的值.
此刻,我收到了该错误消息,无法解决.
At the moment, I have that error message and I can't manage to solve it.
结构字段标记
xml:"test \:isValid,attr
与reflect.StructTag.Get不兼容:结构标记值的语法错误
struct field tag
xml:"test\:isValid,attr
not compatible with reflect.StructTag.Get: bad syntax for struct tag value
我也尝试了以下值
type Event struct {
IsValid string `xml:"test isValid,attr`
}
和
type Event struct {
IsValid string `xml:"test\:isValid,attr`
}
但是效果不佳.
推荐答案
您可以在标签定义中省略"test:"
前缀.只要确保您的XML有效,您的XML就没有< event>
的结束标记,并且没有匹配的结束标记</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示例:
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)
输出(在转到游乐场上尝试):
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"`
}
解析代码是相同的.输出(在进入游乐场上尝试):
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以归因于“:"在标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!