XML 编码:混合属性和元素
XML encoding: mix attributes and elements
我有一个关于编组 Go 的问题 XML:我明白了:
<root abc="">
<element></element>
</root>
但我想要这样:
<root>
<element abc=""></element>
</root>
(属性abc
在子元素处)
这(很容易)可能吗?
我的代码:
package main
import (
"encoding/xml"
"fmt"
"os"
)
type foo struct {
XMLName xml.Name `xml:"root"`
Abc string `xml:"abc,attr"`
Element string `xml:"element"`
}
func main() {
f := foo{}
a, err := xml.MarshalIndent(f, "", " ")
if err != nil {
fmt.Println(err)
os.Exit(0)
}
fmt.Println(string(a))
}
您可以像这样定义您的结构:
type foo struct {
XMLName xml.Name `xml:"root"`
Element struct{
xml.Name `xml:"element"`
Abc string `xml:"abc,attr"`
}
}
我有一个关于编组 Go 的问题 XML:我明白了:
<root abc="">
<element></element>
</root>
但我想要这样:
<root>
<element abc=""></element>
</root>
(属性abc
在子元素处)
这(很容易)可能吗?
我的代码:
package main
import (
"encoding/xml"
"fmt"
"os"
)
type foo struct {
XMLName xml.Name `xml:"root"`
Abc string `xml:"abc,attr"`
Element string `xml:"element"`
}
func main() {
f := foo{}
a, err := xml.MarshalIndent(f, "", " ")
if err != nil {
fmt.Println(err)
os.Exit(0)
}
fmt.Println(string(a))
}
您可以像这样定义您的结构:
type foo struct {
XMLName xml.Name `xml:"root"`
Element struct{
xml.Name `xml:"element"`
Abc string `xml:"abc,attr"`
}
}