duanre1891 2015-11-06 00:30
浏览 27

使用Go解组嵌套XML

I have the following snippet of code that I have been banging my head on the wall trying to make it work. I have searched everywhere for a solution, but none of the ones that I have found seem to work.

It seems that I have an issue with my mapping for the xml.Unmarshal command as it pertains to nested fields. The code below works for retrieving the first value which is called unit, and is on the top level of the xml code.

The other two fields come up as zero, and they are nested two level deep. That implies that the structure isn't set up correctly. Here is the code.

package main

import (
    "encoding/xml"
    "fmt"
)

type datevalue struct {
    Date  int     `xml:"date"`
    Value float32 `xml:"value"`
}

type pv struct {
    XMLName    xml.Name  `xml:"series"`
    Unit       string    `xml:"unit"`
    datevalues datevalue `xml:"values>dateValue"`
}

func main() {
    contents := `<series>
                   <timeUnit>DAY</timeUnit>
                   <unit>Wh</unit><measuredBy>INVERTER</measuredBy>
                   <values><dateValue>
                        <date>2015-11-04 00:00:00</date>
                        <value>5935.405</value>
                   </dateValue></values>
                </series>`

    m := &pv{}
    xml.Unmarshal([]byte(contents), &m)
    fmt.Printf("%s %f %d
", m.Unit, m.datevalues.Value, m.datevalues.Date)
}

And here is the output:

Wh 0.000000 0
  • 写回答

1条回答 默认 最新

  • douzhao1912 2015-11-06 06:38
    关注

    First of all your code doesn't work because you should use exported fields for marshalling/unmarshalling (see https://golang.org/pkg/encoding/xml/).
    You should use

    type pv struct {
        XMLName    xml.Name  `xml:"series"`
        Unit       string    `xml:"unit"`
        Datevalues datevalue `xml:"values>dateValue"`
    }
    

    instead of

    type pv struct {
        XMLName    xml.Name  `xml:"series"`
        Unit       string    `xml:"unit"`
        datevalues datevalue `xml:"values>dateValue"`
    }
    

    Look at DateValues field name. If first symbol is uppercased it's will be exported. Otherwise that field will be ignored while Unmarshal

    Second:

    After it I noticed that you are ignoring your errors. Please don't ignore them, they are critically useful.

    Check it out on the <kbd>go playgroung</kbd>

    As you can see you use int datatype for Date field of datatype. If you change type to string, your code will work.

    Third:

    I think you really want to unmarshall your date value into time.Time.
    To do so you can check this related question

    The complete working code you can try on the <kbd>go playground</kbd>

    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题