douxin20081125 2017-11-06 13:33 采纳率: 0%
浏览 62
已采纳

解组XML注释

I am trying to unmarshal XML from RRD format.

So I create my types etc, etc. which I am able to get the values from.

But there are comments in the XML in this format <!-- 2017/01/01 --> and I need to extract this date. Is there any way of accessing this in GO ?

Thanks.

Updated question:

Okay I did it works, but I want to split the comments into array.

For example I have the following XML.

<database>
<!-- Random Info. -->
<row>10101</row>
<!-- Random Info2 . -->
<row>10102</row>
</database>

So I have the following.

type Database struct {
          Comment string `xml:",comment"`   
          Row []string  `xml:"row"`
    }

Now when I print the row data I get it as an array whereas the comment is a string. I tried to make it an array but it throws an error cannot convert. panic: reflect.Set: value of type []uint8 is not assignable to type []string

Obviously, I can split the string and get what I want. But is there any quicker way of doing this when creating type?

  • 写回答

1条回答 默认 最新

  • dqxsuig64994 2017-11-06 13:36
    关注

    Have you tried https://golang.org/pkg/encoding/xml/#Unmarshal?

    If the XML element contains comments, they are accumulated in the first struct field that has tag ",comment". The struct field may have type []byte or string. If there is no such field, the comments are discarded.

    Here's usage example: https://golang.org/src/encoding/xml/example_test.go

    type Person struct {
        XMLName   xml.Name `xml:"person"`
        ...
        Comment   string   `xml:",comment"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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端口转发问题
  • ¥15 帮我写一个c++工程