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

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

Avoid XML integer parse errors if field empty

问题

考虑这两个XML文档

  1. <a>
  2. <b nil="true"></b>
  3. </a>

  1. <a>
  2. <b type="integer">1</b>
  3. </a>

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

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

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

英文:

Consider these 2 XML documents

  1. &lt;a&gt;
  2. &lt;b nil=&quot;true&quot;&gt;&lt;/b&gt;
  3. &lt;/a&gt;

and

  1. &lt;a&gt;
  2. &lt;b type=&quot;integer&quot;&gt;1&lt;/b&gt;
  3. &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"包。它有助于:

  1. package main
  2. import (
  3. "fmt"
  4. "encoding/xml"
  5. "github.com/guregu/null"
  6. )
  7. type Items struct{
  8. It []Item `xml:"Item"`
  9. }
  10. type Item struct {
  11. DealNo string
  12. ItemNo null.Int
  13. Name string
  14. Price null.Float
  15. Quantity null.Float
  16. Place string
  17. }
  18. func main(){
  19. data := `
  20. <Items>
  21. <Item>
  22. <DealNo/>
  23. <ItemNo>32435</ItemNo>
  24. <Name>dsffdf</Name>
  25. <Price>135.12</Price>
  26. <Quantity></Quantity>
  27. <Place>dsffs</Place>
  28. </Item>
  29. <Item>
  30. <DealNo/>
  31. <ItemNo></ItemNo>
  32. <Name>dsfsfs</Name>
  33. <Price></Price>
  34. <Quantity>1.5</Quantity>
  35. <Place>sfsfsfs</Place>
  36. </Item>
  37. </Items>`
  38. var xmlMsg Items
  39. if err := xml.Unmarshal([]byte(data), &xmlMsg); err != nil {
  40. fmt.Println("Error: cannot unmarshal xml from web ", err)
  41. } else {
  42. fmt.Println("XML: ", xmlMsg)
  43. }
  44. }
英文:

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

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;encoding/xml&quot;
  5. &quot;github.com/guregu/null&quot;
  6. )
  7. type Items struct{
  8. It []Item `xml:&quot;Item&quot;`
  9. }
  10. type Item struct {
  11. DealNo string
  12. ItemNo null.Int
  13. Name string
  14. Price null.Float
  15. Quantity null.Float
  16. Place string
  17. }
  18. func main(){
  19. data := `
  20. &lt;Items&gt;
  21. &lt;Item&gt;
  22. &lt;DealNo/&gt;
  23. &lt;ItemNo&gt;32435&lt;/ItemNo&gt;
  24. &lt;Name&gt;dsffdf&lt;/Name&gt;
  25. &lt;Price&gt;135.12&lt;/Price&gt;
  26. &lt;Quantity&gt;&lt;/Quantity&gt;
  27. &lt;Place&gt;dsffs&lt;/Place&gt;
  28. &lt;/Item&gt;
  29. &lt;Item&gt;
  30. &lt;DealNo/&gt;
  31. &lt;ItemNo&gt;&lt;/ItemNo&gt;
  32. &lt;Name&gt;dsfsfs&lt;/Name&gt;
  33. &lt;Price&gt;&lt;/Price&gt;
  34. &lt;Quantity&gt;1.5&lt;/Quantity&gt;
  35. &lt;Place&gt;sfsfsfs&lt;/Place&gt;
  36. &lt;/Item&gt;
  37. &lt;/Items&gt;`
  38. var xmlMsg Items
  39. if err := xml.Unmarshal([]byte(data), &amp;xmlMsg); err != nil {
  40. fmt.Println(&quot;Error: cannot unmarshal xml from web &quot;, err)
  41. } else {
  42. fmt.Println(&quot;XML: &quot;, xmlMsg)
  43. }
  44. }

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:

确定