【发布时间】:2017-01-21 07:41:57
【问题描述】:
当我解组和编组此 XML 时,命名空间的 URL 消失:
<root xmlns:urn="http://test.example.com">
<urn:copyright>tekst</urn:copyright>
</root>
变成:
<root xmlns:urn="">
<urn:copyright></urn:copyright>
</root>
代码:
package main
import (
"encoding/xml"
"fmt"
)
type Root struct {
XMLName xml.Name `xml:"root"`
XmlNS string `xml:"xmlns:urn,attr"`
Copyright Copyright `xml:"urn:copyright,omitempty"`
}
type Copyright struct {
Text string `xml:",chardata"`
}
func main() {
root := Root{}
x := `<root xmlns:urn="http://test.example.com">
<urn:copyright>text</urn:copyright>
</root>`
_ = xml.Unmarshal([]byte(x), &root)
b, _ := xml.MarshalIndent(root, "", " ")
fmt.Println(string(b))
}
【问题讨论】:
标签: xml go marshalling unmarshalling