我想知道是否可以使用中的Unmarshal
方法获取XML名称空间前缀encoding/xml
。
例如,我有:
<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema">
</application>
我想知道如何检索xs
XMLSchema 的定义前缀,而不必使用该Token
方法。
就像其他所有属性一样获得它:
type App struct {
XS string `xml:"xs,attr"`
}
游乐场:http : //play.golang.org/p/2IOmkX1Jov。
如果您还具有实际xs
属性sans,它将变得更加棘手xmlns
。即使将名称空间URI添加到XS
的标签中,也可能会出现错误。
编辑:如果要获取所有声明的名称空间,则可以UnmarshalXML
在元素上定义一个自定义并扫描其属性:
type App struct {
Namespaces map[string]string
Foo int `xml:"foo"`
}
func (a *App) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
a.Namespaces = map[string]string{}
for _, attr := range start.Attr {
if attr.Name.Space == "xmlns" {
a.Namespaces[attr.Name.Local] = attr.Value
}
}
// Go on with unmarshalling.
type app App
aa := (*app)(a)
return d.DecodeElement(aa, &start)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句