当我解组和编组这个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))
}发布于 2017-01-24 10:04:45
Root.XmlNS不是解组的。
"The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/." https://www.w3.org/TR/REC-xml-names/
您可能希望执行以下操作。
type Root struct {
XMLName xml.Name `xml:"root"`
Copyright Copyright `xml:"http://test.example.com copyright"`
}https://stackoverflow.com/questions/41776930
复制相似问题