首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Go中封送XML时保留标记的命名空间url。

如何在Go中封送XML时保留标记的命名空间url。
EN

Stack Overflow用户
提问于 2017-01-21 15:41:58
回答 1查看 453关注 0票数 0

当我解组和编组这个XML时,名称空间的URL消失了:

代码语言:javascript
复制
<root xmlns:urn="http://test.example.com">
    <urn:copyright>tekst</urn:copyright>
</root>

变成:

代码语言:javascript
复制
<root xmlns:urn="">
    <urn:copyright></urn:copyright>
</root>

代码:

代码语言:javascript
复制
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))
}

https://play.golang.org/p/1jSURtWuZO

EN

回答 1

Stack Overflow用户

发布于 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/

您可能希望执行以下操作。

代码语言:javascript
复制
    type Root struct {
        XMLName      xml.Name     `xml:"root"`
        Copyright    Copyright    `xml:"http://test.example.com copyright"`
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41776930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档