XML Marshal在这个Go示例中不起作用。

huangapple go评论164阅读模式
英文:

XML Marshal not working in this Go example

问题

在这段代码中,返回的元素x没有正文 - 我认为MarshalIndent没有正常工作。
我将无法解析结构体Record。
是否有任何解决方法,以便可以按预期返回值。

package main

import "fmt"
import "encoding/xml"
import "time"

type Record struct {
    a int64     `xml:"a,omitempty"`
    b int64     `xml:"b,omitempty"`
    c int64     `xml:"c,omitempty"`
    d int64     `xml:"d,omitempty"`
    e int64     `xml:"e,omitempty"`
    f string    `xml:"f,omitempty"`
    g string    `xml:"g,omitempty"`
    h string    `xml:"h,omitempty"`
    i string    `xml:"i,omitempty"`
    j string    `xml:"j,omitempty"`
    k time.Time `xml:"k,omitempty"`
    l time.Time `xml:"l,omitempty"`
    m string    `xml:"m,omitempty"`
    n string    `xml:"n,omitempty"`
    o string    `xml:"o,omitempty"`
    p int64     `xml:"p,omitempty"`
}

func main() {
    temp, _ := time.Parse(time.RFC3339, "")
    candiateXML := &Record{1, 2, 3, 4, 5, "6", "7", "8", "9", "10", temp, temp, "13", "14", "15", 16}
    fmt.Printf("%v\n", candiateXML.a)
    fmt.Printf("%v\n", candiateXML.b)
    fmt.Printf("%v\n", candiateXML.c)
    fmt.Printf("%v\n", candiateXML.d)
    fmt.Printf("%v\n", candiateXML.e)
    fmt.Printf("%s\n", candiateXML.f)
    fmt.Printf("%s\n", candiateXML.g)
    fmt.Printf("%s\n", candiateXML.h)
    fmt.Printf("%s\n", candiateXML.i)
    fmt.Printf("%s\n", candiateXML.j)
    fmt.Printf("%d\n", candiateXML.k)
    fmt.Printf("%d\n", candiateXML.l)
    fmt.Printf("%s\n", candiateXML.m)
    fmt.Printf("%s\n", candiateXML.n)
    fmt.Printf("%s\n", candiateXML.o)
    fmt.Printf("%v\n", candiateXML.p)

    x, err := xml.MarshalIndent(candiateXML, "", "  ")
    if err != nil {
        return
    }
    //为什么这个没有正确打印
    fmt.Printf("%s\n", x)
}

MarshalIndent没有返回预期的结果。

英文:

In this code the returned element x does not have body - I believe the MarshalIndent is not working properly.
I will not be able the struct Record.
Is there any work around so that this can return the value as expected.

package main
import "fmt"
import "encoding/xml"
import "time"
type Record struct {
a int64     `xml:"a,omitempty"`
b int64     `xml:"b,omitempty"`
c int64     `xml:"c,omitempty"`
d int64     `xml:"d,omitempty"`
e int64     `xml:"e,omitempty"`
f string    `xml:"f,omitempty"`
g string    `xml:"g,omitempty"`
h string    `xml:"h,omitempty"`
i string    `xml:"i,omitempty"`
j string    `xml:"j,omitempty"`
k time.Time `xml:"k,omitempty"`
l time.Time `xml:"l,omitempty"`
m string    `xml:"m,omitempty"`
n string    `xml:"n,omitempty"`
o string    `xml:"o,omitempty"`
p int64     `xml:"p,omitempty"`
}
func main() {
temp, _ := time.Parse(time.RFC3339, "")
candiateXML := &Record{1, 2, 3, 4, 5, "6", "7", "8", "9", "10", temp, temp, "13", "14", "15", 16}
fmt.Printf("%v\n", candiateXML.a)
fmt.Printf("%v\n", candiateXML.b)
fmt.Printf("%v\n", candiateXML.c)
fmt.Printf("%v\n", candiateXML.d)
fmt.Printf("%v\n", candiateXML.e)
fmt.Printf("%s\n", candiateXML.f)
fmt.Printf("%s\n", candiateXML.g)
fmt.Printf("%s\n", candiateXML.h)
fmt.Printf("%s\n", candiateXML.i)
fmt.Printf("%s\n", candiateXML.j)
fmt.Printf("%d\n", candiateXML.k)
fmt.Printf("%d\n", candiateXML.l)
fmt.Printf("%s\n", candiateXML.m)
fmt.Printf("%s\n", candiateXML.n)
fmt.Printf("%s\n", candiateXML.o)
fmt.Printf("%v\n", candiateXML.p)
x, err := xml.MarshalIndent(candiateXML, "", "  ")
if err != nil {
return
}
//why this is not printing properly
fmt.Printf("%s\n", x)
}

The MarshalIndent does not return the expected result.

答案1

得分: 1

《Go编程语言规范》

导出标识符

为了允许从另一个包中访问标识符,可以将其导出。如果标识符同时满足以下两个条件,则它是导出的:

  1. 标识符名称的第一个字符是一个Unicode大写字母(Unicode类别"Lu");
  2. 标识符在包块中声明,或者它是字段名或方法名。

所有其他标识符都不是导出的。

将你的Record结构体字段(首字母大写)导出,以允许从xml包中访问它们。例如:

package main

import "fmt"
import "encoding/xml"

type Record struct {
    A int64 `xml:"a,omitempty"`
    B int64 `xml:"b,omitempty"`
    C int64 `xml:"c,omitempty"`
}

func main() {
    candiateXML := &Record{1, 2, 3}
    fmt.Printf("%v\n", candiateXML.A)
    fmt.Printf("%v\n", candiateXML.B)
    fmt.Printf("%v\n", candiateXML.C)

    x, err := xml.MarshalIndent(candiateXML, "", "  ")
    if err != nil {
        return
    }
    fmt.Printf("%s\n", x)
}

输出:

1
2
3
<Record>
<a>1</a>
<b>2</b>
<c>3</c>
</Record>
英文:

> The Go Programming Language Specification
>
> Exported identifiers
>
> An identifier may be exported to permit access to it from another
> package. An identifier is exported if both:
>
> 1. the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
> 2. the identifier is declared in the package block or it is a field name or method name.
>
> All other identifiers are not exported.

Export your Record struct field names (first character upper case) to permit access to them from the xml package. For example,

package main
import &quot;fmt&quot;
import &quot;encoding/xml&quot;
type Record struct {
A int64 `xml:&quot;a,omitempty&quot;`
B int64 `xml:&quot;b,omitempty&quot;`
C int64 `xml:&quot;c,omitempty&quot;`
}
func main() {
candiateXML := &amp;Record{1, 2, 3}
fmt.Printf(&quot;%v\n&quot;, candiateXML.A)
fmt.Printf(&quot;%v\n&quot;, candiateXML.B)
fmt.Printf(&quot;%v\n&quot;, candiateXML.C)
x, err := xml.MarshalIndent(candiateXML, &quot;&quot;, &quot;  &quot;)
if err != nil {
return
}
fmt.Printf(&quot;%s\n&quot;, x)
}

Output:

1
2
3
&lt;Record&gt;
&lt;a&gt;1&lt;/a&gt;
&lt;b&gt;2&lt;/b&gt;
&lt;c&gt;3&lt;/c&gt;
&lt;/Record&gt;

huangapple
  • 本文由 发表于 2017年5月30日 06:24:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/44250935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定