Golang如果为空,则隐藏XML父标签

huangapple go评论76阅读模式
英文:

Golang Hide XML parent tag if empty

问题

经过一些尝试和错误,我想分享一下我正在处理的问题。

我正在填充一个结构体并将其转换为 XML(使用 xml.Marshal)。
如下所示,Foo示例按预期工作。然而,Bar示例创建了一个空的Group1。

所以我的问题是:“如果没有设置子元素,如何防止生成Group1。”

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输出:

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

Bar输出:

<Example1>
    <Group1></Group1>  <------ 如何删除空的父元素?
    <Group2>
        <Example3>Value3</Example3>
    </Group2>
</Example1>

补充

此外,我尝试了以下操作,但仍然生成了一个空的“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"`
}

完整的代码可以在这里找到:http://play.golang.org/p/SHIcBHoLCG

编辑:更改了Golang示例,使用MarshalIndent以提高可读性。

编辑2 Ainar-G的示例可以很好地隐藏空的父元素,但是填充它会变得更加困难。出现“panic: runtime error: invalid memory address or nil pointer dereference”错误。

英文:

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 (
    &quot;fmt&quot;
    &quot;encoding/xml&quot;
)

type Example1 struct{
    XMLName  xml.Name `xml:&quot;Example1&quot;`
    Element1 string   `xml:&quot;Group1&gt;Element1,omitempty&quot;`
    Element2 string   `xml:&quot;Group1&gt;Element2,omitempty&quot;`
    Element3 string   `xml:&quot;Group2&gt;Example3,omitempty&quot;`
}

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

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

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

Foo Output :

&lt;Example1&gt;
    &lt;Group1&gt;
        &lt;Element1&gt;Value1&lt;/Element1&gt;
        &lt;Element2&gt;Value2&lt;/Element2&gt;
    &lt;/Group1&gt;
    &lt;Group2&gt;
        &lt;Example3&gt;Value3&lt;/Example3&gt;
    &lt;/Group2&gt;
&lt;/Example1&gt;

Bar Output :

&lt;Example1&gt;
    &lt;Group1&gt;&lt;/Group1&gt;  &lt;------ How to remove the empty parent value ? 
    &lt;Group2&gt;
        &lt;Example3&gt;Value3&lt;/Example3&gt;
    &lt;/Group2&gt;
&lt;/Example1&gt;

Addition

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

type Example2 struct{
    XMLName  xml.Name `xml:&quot;Example2&quot;`
    Group1   struct{
        XMLName  xml.Name `xml:&quot;Group1,omitempty&quot;`
        Element1 string   `xml:&quot;Element1,omitempty&quot;`
        Element2 string   `xml:&quot;Element2,omitempty&quot;`
    }
    Element3 string   `xml:&quot;Group2&gt;Example3,omitempty&quot;`
}

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

得分: 28

Example1不起作用,因为显然omitempty标签只对元素本身起作用,而不是a>b>c封闭元素。

Example2不起作用,因为omitempty无法将空结构体识别为空。来自文档

空值包括false、0、任何nil指针或接口值,以及长度为零的任何数组、切片、映射或字符串。

没有提到结构体。你可以通过将Group1更改为指向结构体的指针来使baz示例起作用:

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"`
}

然后,如果你想填充Group1,你需要单独分配它:

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

示例:http://play.golang.org/p/mgpI4OsHf7

英文:

Example1 doesn't work because apparently the ,omitempty tag only works on the element itself and not the a&gt;b&gt;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:&quot;Example1&quot;`
	Group1   *Group1
	Element3 string `xml:&quot;Group2&gt;Example3,omitempty&quot;`
}

type Group1 struct {
	XMLName  xml.Name `xml:&quot;Group1,omitempty&quot;`
	Element1 string   `xml:&quot;Element1,omitempty&quot;`
	Element2 string   `xml:&quot;Element2,omitempty&quot;`
}

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

foo.Group1 = &amp;Group1{
	Element1: &quot;Value1&quot;,
}

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

huangapple
  • 本文由 发表于 2014年12月2日 17:46:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/27246275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定