dongyakui8675 2014-12-02 09:46
浏览 29
已采纳

Golang隐藏XML父标记(如果为空)

After some trail and error I'd like to share the issue I am dealing with.

I'm populating an struct and convert it to an XML ( xml.Marshal ) As you can see below the Foo example works as expected. The Bar example however creates an empty group1.

So my question is : "How do I prevent Group1 to be generated if there are no children set."

package main

import (
    "fmt"
    "encoding/xml"
)

type Example1 struct{
    XMLName  xml.Name `xml:"Example1"`
    Element1 string   `xml:"Group1>Element1,omitempty"`
    Element2 string   `xml:"Group1>Element2,omitempty"`
    Element3 string   `xml:"Group2>Example3,omitempty"`
}

func main() {
    foo := &Example1{}
    foo.Element1 = "Value1" 
    foo.Element2 = "Value2" 
    foo.Element3 = "Value3" 

    fooOut, _ := xml.Marshal(foo)
    fmt.Println( string(fooOut) )

    bar  := &Example1{}
    bar.Element3 = "Value3"
    barOut, _ := xml.Marshal(bar)
    fmt.Println( string(barOut) )
}

Foo Output :

<Example1>
    <Group1>
        <Element1>Value1</Element1>
        <Element2>Value2</Element2>
    </Group1>
    <Group2>
        <Example3>Value3</Example3>
    </Group2>
</Example1>

Bar Output :

<Example1>
    <Group1></Group1>  <------ How to remove the empty parent value ? 
    <Group2>
        <Example3>Value3</Example3>
    </Group2>
</Example1>

Addition

Additionally i have tried doing the following, but still generates an empty "Group1":

type Example2 struct{
    XMLName  xml.Name `xml:"Example2"`
    Group1   struct{
        XMLName  xml.Name `xml:"Group1,omitempty"`
        Element1 string   `xml:"Element1,omitempty"`
        Element2 string   `xml:"Element2,omitempty"`
    }
    Element3 string   `xml:"Group2>Example3,omitempty"`
}

The full code can be found here : http://play.golang.org/p/SHIcBHoLCG . example to

EDIT : Changed the golang example to use MarshalIndent for readability

Edit 2 The example from Ainar-G Works good for hiding the empty parent, but Populating it makes it a lot harder. "panic: runtime error: invalid memory address or nil pointer dereference"

  • 写回答

1条回答 默认 最新

  • duanci3845 2014-12-02 10:01
    关注

    Example1 doesn't work because apparently the ,omitempty tag only works on the element itself and not the a>b>c enclosing elements.

    Example2 doesn't work because ,omitempty doesn't recognise empty structs as empty. From the doc:

    The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero.

    No mention of structs. You can make the baz example work by changing Group1 to a pointer to a struct:

    type Example2 struct {
        XMLName  xml.Name `xml:"Example1"`
        Group1   *Group1
        Element3 string `xml:"Group2>Example3,omitempty"`
    }
    
    type Group1 struct {
        XMLName  xml.Name `xml:"Group1,omitempty"`
        Element1 string   `xml:"Element1,omitempty"`
        Element2 string   `xml:"Element2,omitempty"`
    }
    

    Then, if you want to fill Group1, you'll need to allocate it separately:

    foo.Group1 = &Group1{
        Element1: "Value1",
    }
    

    Example: http://play.golang.org/p/mgpI4OsHf7

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应