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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图