英文:
Go - Unmarshal XML, Attribute issues
问题
也许有人可以在这里提供一些见解...我在编码/XML库方面遇到了一些困难。
我无论如何都无法复制< gpx >中的有效XML属性。
基本上,我正在将XML数据从GPS文件中解组,然后将其重新组合到另一个文件中。一切都正常工作,除了根XML< gpx >的属性标签。
我尝试了各种方法,如:
func (c *gpx) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {}
但都无济于事。
基本上,我只是希望根标签< GPX >能够正确地分配所有属性。为什么不能使用Attributes []xml.Attr xml:",attr"或类似的方式,这超出了我的理解。
良好的XML头 -> http://pastebin.com/XjEZuBa1
我无法链接错误的XML头,因为我是一个新成员...但XML解组/组合过程会在命名空间中添加_,这会导致问题,还有其他一些问题。
GO Playground链接:http://play.golang.org/p/J7wy6306Cj
非常感谢任何帮助!
谢谢。
英文:
Maybe somebody could give some insight here... I seem to be hitting a brick wall with the encoding/XML library.
For the life of me, I can't replicate the valid XML attributes from < gpx >
Basically i'm unmarshalling the XML data from a GPS file, then marshalling it back into another file. Everything is working correctly, except the attribute tags for the root XML < gpx >
I've tried various
func (c *gpx) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {}
type approaches to no avail.
Basically I just want the root tag < GPX > to have all the attributes assigned correctly. Why you can't do Attributes []xml.Attr xml:",attr"or something similar is beyond me.
Good XML header -> http://pastebin.com/XjEZuBa1
I can't link the bad XML header, since I'm a new member.. but the XML unmarshal/marshal process adds _ to the namespace which causes issues, among other things.
GO Playground link: http://play.golang.org/p/J7wy6306Cj
Any help would be greatly appreciated,
Thank you.
答案1
得分: 0
很抱歉,Go的默认XML编码器无法编码像这样的内容:
xmlns:foo="http://example.com/Foo-V1" foo:attr="bar"
正如代码所示,它根据URL选择名称,你不能自己定义命名空间。Go编码器生成的代码类似于上面的代码,据我所知,它基本上等同于上面的代码:
xmlns:Foo-V1="http://example.com/Foo-V1" Foo-V1:attr="bar"
这里唯一不同的是命名空间的前缀。
至于其他命名空间的前向声明,我建议只在需要它们的元素和属性上声明。例如,要编码像这样的内容:
<foo xmlns:bar="http://example.com/Bar-V1">
<bar:elem>Hello world</bar:elem>
</foo>
可以使用以下结构体:
type Foo struct {
XMLName xml.Name `xml:"foo"`
BarElem BarElem
}
type BarElem struct {
XMLName xml.Name `xml:"http://example.com/Bar-V1 elem"`
Data string `xml:",innerxml"`
}
它序列化为:
<foo>
<elem xmlns="http://example.com/Bar-V1">Hello world</elem>
</foo>
Playground: http://play.golang.org/p/79bhk70yFj.
英文:
Unfortunately, the default Go XML encoder cannot encode stuff like
xmlns:foo="http://example.com/Foo-V1" foo:attr="bar"
As the code shows, it chooses the name based on the URL, and you can't just define namespaces yourself. The Go encoder emits code like this which, AFAIK, is basically equivalent to the one above:
xmlns:Foo-V1="http://example.com/Foo-V1" Foo-V1:attr="bar"
The only thing that is different here is the prefix for the namespace.
As for forward-declaration of other namespaces, I'd suggest only declaring them on elements and attributes that need them. I.e. to encode something like
<foo xmlns:bar="http://example.com/Bar-V1">
<bar:elem>Hello world</bar:elem>
</foo>
use structs like this
type Foo struct {
XMLName xml.Name `xml:"foo"`
BarElem BarElem
}
type BarElem struct {
XMLName xml.Name `xml:"http://example.com/Bar-V1 elem"`
Data string `xml:",innerxml"`
}
which serializes to
<foo>
<elem xmlns="http://example.com/Bar-V1">Hello world</elem>
</foo>
Playground: http://play.golang.org/p/79bhk70yFj.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论