如果字段为空,则避免XML整数解析错误

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

Avoid XML integer parse errors if field empty

问题

考虑这两个XML文档

<a>
  <b nil="true"></b>
</a>

<a>
  <b type="integer">1</b>
</a>

如何在Go中正确地将此XML解组为类型为int的b结构字段,而不会在第一种情况下产生strconv.ParseInt: parsing "": invalid syntax错误?

在这种情况下,omitempty似乎不起作用。

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

英文:

Consider these 2 XML documents

&lt;a&gt;
  &lt;b nil=&quot;true&quot;&gt;&lt;/b&gt;
&lt;/a&gt;

and

&lt;a&gt;
  &lt;b type=&quot;integer&quot;&gt;1&lt;/b&gt;
&lt;/a&gt;

How can I unmarshal this XML properly in Go to a b struct field of type int, without producing a strconv.ParseInt: parsing &quot;&quot;: invalid syntax error in the first case?

omitempty doesn't seem to work in this case.

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

答案1

得分: 1

The omitempty tag is just respected with Marshal, not Unmarshal.

Unmarshal errors if the int value is not an actual int.

Instead, change B to a string. Then, convert B to an int with the strconv package. If it errors, set it to 0.

Try this snippet: http://play.golang.org/p/1zqmlmIQDB

英文:

The omitempty tag is just respected with Marshal, not Unmarshal.

Unmarshal errors if the int value is not an actual int.

Instead, change B to a string. Then, convert B to an int with the strconv package. If it errors, set it to 0.

Try this snippet: http://play.golang.org/p/1zqmlmIQDB

答案2

得分: 0

你可以使用"github.com/guregu/null"包。它有助于:

package main
import (
"fmt"
    "encoding/xml"
    "github.com/guregu/null"
)

type Items struct{
    It []Item `xml:"Item"`
}

type Item struct {
    DealNo   string
    ItemNo   null.Int
    Name     string
    Price    null.Float
    Quantity null.Float
    Place    string
}

func main(){
    data := `
    <Items>
        <Item>
                <DealNo/>
                <ItemNo>32435</ItemNo>
                <Name>dsffdf</Name>
                <Price>135.12</Price>
                <Quantity></Quantity>
                <Place>dsffs</Place>
            </Item>
            <Item>
                <DealNo/>
                <ItemNo></ItemNo>
                <Name>dsfsfs</Name>
                <Price></Price>
                <Quantity>1.5</Quantity>
                <Place>sfsfsfs</Place>
            </Item>
            </Items>`

var xmlMsg Items

        if err := xml.Unmarshal([]byte(data), &xmlMsg); err != nil {
    fmt.Println("Error: cannot unmarshal xml from web ", err)
    } else {
        fmt.Println("XML: ", xmlMsg)
    }
}
英文:

You can use "github.com/guregu/null" package. It helps:

package main
import (
&quot;fmt&quot;
	&quot;encoding/xml&quot;
	&quot;github.com/guregu/null&quot;
)

type Items struct{
	It []Item `xml:&quot;Item&quot;`
}

type Item struct {
	DealNo   string
	ItemNo   null.Int
	Name     string
	Price    null.Float
	Quantity null.Float
	Place    string
}

func main(){
	data := `
	&lt;Items&gt;
		&lt;Item&gt;
	            &lt;DealNo/&gt;
	            &lt;ItemNo&gt;32435&lt;/ItemNo&gt;
	            &lt;Name&gt;dsffdf&lt;/Name&gt;
	            &lt;Price&gt;135.12&lt;/Price&gt;
	            &lt;Quantity&gt;&lt;/Quantity&gt;
	            &lt;Place&gt;dsffs&lt;/Place&gt;
	        &lt;/Item&gt;
	        &lt;Item&gt;
	            &lt;DealNo/&gt;
	            &lt;ItemNo&gt;&lt;/ItemNo&gt;
	            &lt;Name&gt;dsfsfs&lt;/Name&gt;
	            &lt;Price&gt;&lt;/Price&gt;
	            &lt;Quantity&gt;1.5&lt;/Quantity&gt;
	            &lt;Place&gt;sfsfsfs&lt;/Place&gt;
	        &lt;/Item&gt;
			&lt;/Items&gt;`

var xmlMsg Items

        if err := xml.Unmarshal([]byte(data), &amp;xmlMsg); err != nil {
		fmt.Println(&quot;Error: cannot unmarshal xml from web &quot;, err)
		} else {
			fmt.Println(&quot;XML: &quot;, xmlMsg)
		}
}

huangapple
  • 本文由 发表于 2013年7月10日 21:42:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/17572220.html
匿名

发表评论

匿名网友

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

确定