doufang6268 2017-05-29 22:24
浏览 47
已采纳

XML Marshal在此Go示例中不起作用

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
", candiateXML.a)
    fmt.Printf("%v
", candiateXML.b)
    fmt.Printf("%v
", candiateXML.c)
    fmt.Printf("%v
", candiateXML.d)
    fmt.Printf("%v
", candiateXML.e)
    fmt.Printf("%s
", candiateXML.f)
    fmt.Printf("%s
", candiateXML.g)
    fmt.Printf("%s
", candiateXML.h)
    fmt.Printf("%s
", candiateXML.i)
    fmt.Printf("%s
", candiateXML.j)
    fmt.Printf("%d
", candiateXML.k)
    fmt.Printf("%d
", candiateXML.l)
    fmt.Printf("%s
", candiateXML.m)
    fmt.Printf("%s
", candiateXML.n)
    fmt.Printf("%s
", candiateXML.o)
    fmt.Printf("%v
", candiateXML.p)

    x, err := xml.MarshalIndent(candiateXML, "", "  ")
    if err != nil {
        return
    }
    //why this is not printing properly
    fmt.Printf("%s
", x)
}

The MarshalIndent does not return the expected result.

  • 写回答

1条回答 默认 最新

  • duanjiao6730 2017-05-29 22:40
    关注

    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 "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
    ", candiateXML.A)
        fmt.Printf("%v
    ", candiateXML.B)
        fmt.Printf("%v
    ", candiateXML.C)
    
        x, err := xml.MarshalIndent(candiateXML, "", "  ")
        if err != nil {
            return
        }
        fmt.Printf("%s
    ", x)
    }
    

    Output:

    1
    2
    3
    <Record>
      <a>1</a>
      <b>2</b>
      <c>3</c>
    </Record>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?