how to Extracting data from XML

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

how to Extracting data from XML

问题

我正在从一个 XML 输出中提取数据。我已经编写了下面的代码。我只需要从下面的 XML 中提取部门编号。运行下面的代码时得到一个空输出。有人可以告诉我如何从 XML 中提取部门编号以及为什么输出为空吗?

  1. package main
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. )
  6. type Users struct {
  7. XMLName xml.Name `xml:"users"`
  8. User User `xml:"user"`
  9. }
  10. type User struct {
  11. XMLName xml.Name `xml:"user"`
  12. Type string `xml:"name"`
  13. DD DD `xml:"dd"`
  14. }
  15. type DD struct {
  16. XMLName xml.Name `xml:"dd"`
  17. Number string `xml:"number"`
  18. Description string `xml:"description"`
  19. Type Type `xml:"type"`
  20. Dept Dept `xml:"dept"`
  21. }
  22. type Type struct{
  23. XMLName xml.Name `xml:"type"`
  24. }
  25. type Dept struct {
  26. XMLName xml.Name `xml:"dept"`
  27. Number string `xml:"number"`
  28. Type Type `xml:"type"`
  29. }
  30. func main() {
  31. var users Users
  32. var byteValue = []byte(`<users>
  33. <user>
  34. <type>1</type>
  35. <bu>
  36. <number>123</number>
  37. <id>100</id>
  38. <type>
  39. <code>123</code>
  40. </type>
  41. </bu>
  42. <dd>
  43. <number>1</number>
  44. <description>abc</description>
  45. <type>
  46. <code>12345</code>
  47. <id>qw123<id>
  48. </type>
  49. <dept>
  50. <number>10</number> <<<<<<<<
  51. <type>qw12345</type>
  52. </dept>
  53. </dd>
  54. <bd>
  55. <code>34we5</code>
  56. <id>qw123<id>
  57. </bd>
  58. <OD>
  59. <code>9876</code>
  60. <id>qwerty123<id>
  61. </OD>
  62. </user>
  63. </users>`)
  64. xml.Unmarshal(byteValue, &users)
  65. fmt.Println("Dept Number: " + users.User.DD.Dept.Number)
  66. }
英文:

I am working on extracting data from a xml output.
I have written the below code. I just need to extract dept number from the below xml.
On running the below code getting a null output.
Can someone let me how to extract the dept number from the xml and why I am getting null as output?

  1. package main
  2. import (
  3. &quot;encoding/xml&quot;
  4. &quot;fmt&quot;
  5. )
  6. type Users struct {
  7. XMLName xml.Name `xml:&quot;users&quot;`
  8. User User `xml:&quot;user&quot;`
  9. }
  10. type User struct {
  11. XMLName xml.Name `xml:&quot;user&quot;`
  12. Type string `xml:&quot;name&quot;`
  13. DD DD `xml:&quot;dd&quot;`
  14. }
  15. type DD struct {
  16. XMLName xml.Name `xml:&quot;dd&quot;`
  17. Number string `xml:&quot;number&quot;`
  18. Description string `xml:&quot;description&quot;`
  19. Type Type `xml:&quot;type&quot;`
  20. Dept Dept `xml:&quot;dept&quot;`
  21. }
  22. type Type struct{
  23. XMLName xml.Name `xml:&quot;type&quot;`
  24. }
  25. type Dept struct {
  26. XMLName xml.Name `xml:&quot;dept&quot;`
  27. Number string `xml:&quot;number&quot;`
  28. Type Type `xml:&quot;type&quot;`
  29. }
  30. func main() {
  31. var users Users
  32. var byteValue = []byte(`&lt;users&gt;
  33. &lt;user&gt;
  34. &lt;type&gt;1&lt;/type&gt;
  35. &lt;bu&gt;
  36. &lt;number&gt;123&lt;/number&gt;
  37. &lt;id&gt;100&lt;/id&gt;
  38. &lt;type&gt;
  39. &lt;code&gt;123&lt;/code&gt;
  40. &lt;/type&gt;
  41. &lt;/bu&gt;
  42. &lt;dd&gt;
  43. &lt;number&gt;1&lt;/number&gt;
  44. &lt;description&gt;abc&lt;/description&gt;
  45. &lt;type&gt;
  46. &lt;code&gt;12345&lt;/code&gt;
  47. &lt;id&gt;qw123&lt;id&gt;
  48. &lt;type&gt;
  49. &lt;dept&gt;
  50. &lt;number&gt;10&lt;/number&gt; &lt;&lt;&lt;&lt;&lt;&lt;&lt;
  51. &lt;type&gt;qw12345&lt;/type&gt;
  52. &lt;/dept&gt;
  53. &lt;/dd&gt;
  54. &lt;bd&gt;
  55. &lt;code&gt;34we5&lt;/code&gt;
  56. &lt;id&gt;qw123&lt;id&gt;
  57. &lt;/bd&gt;
  58. &lt;OD&gt;
  59. &lt;code&gt;9876&lt;/code&gt;
  60. &lt;id&gt;qwerty123&lt;id&gt;
  61. &lt;/OD&gt;
  62. &lt;/user&gt;
  63. &lt;/users&gt;`)
  64. xml.Unmarshal(byteValue, &amp;users)
  65. fmt.Println(&quot;Dept Number: &quot; + users.User.DD.Dept.Number)
  66. }

答案1

得分: 2

看起来提供的 XML 是错误的。请尝试使用以下 XML:

  1. <users>
  2. <user>
  3. <type>1</type>
  4. <bu>
  5. <number>123</number>
  6. <id>100</id>
  7. <type>
  8. <code>123</code>
  9. </type>
  10. </bu>
  11. <dd>
  12. <number>1</number>
  13. <description>abc</description>
  14. <type>
  15. <code>12345</code>
  16. <id>qw123</id>
  17. </type>
  18. <dept>
  19. <number>10</number>
  20. <type>qw12345</type>
  21. </dept>
  22. </dd>
  23. <bd>
  24. <code>34we5</code>
  25. <id>qw123</id>
  26. </bd>
  27. <OD>
  28. <code>9876</code>
  29. <id>qwerty123</id>
  30. </OD>
  31. </user>
  32. </users>

您可以在 playground 上查看使用您提供的示例的工作示例。链接:https://play.golang.org/p/4zQsaz5Z_5P

英文:

Looks like the XML provided is wrong. Please do try using below XML

  1. &lt;users&gt;
  2. &lt;user&gt;
  3. &lt;type&gt;1&lt;/type&gt;
  4. &lt;bu&gt;
  5. &lt;number&gt;123&lt;/number&gt;
  6. &lt;id&gt;100&lt;/id&gt;
  7. &lt;type&gt;
  8. &lt;code&gt;123&lt;/code&gt;
  9. &lt;/type&gt;
  10. &lt;/bu&gt;
  11. &lt;dd&gt;
  12. &lt;number&gt;1&lt;/number&gt;
  13. &lt;description&gt;abc&lt;/description&gt;
  14. &lt;type&gt;
  15. &lt;code&gt;12345&lt;/code&gt;
  16. &lt;id&gt;qw123&lt;/id&gt;
  17. &lt;/type&gt;
  18. &lt;dept&gt;
  19. &lt;number&gt;10&lt;/number&gt;
  20. &lt;type&gt;qw12345&lt;/type&gt;
  21. &lt;/dept&gt;
  22. &lt;/dd&gt;
  23. &lt;bd&gt;
  24. &lt;code&gt;34we5&lt;/code&gt;
  25. &lt;id&gt;qw123&lt;/id&gt;
  26. &lt;/bd&gt;
  27. &lt;OD&gt;
  28. &lt;code&gt;9876&lt;/code&gt;
  29. &lt;id&gt;qwerty123&lt;/id&gt;
  30. &lt;/OD&gt;
  31. &lt;/user&gt;
  32. &lt;/users&gt;

You can have a look at the working example with your provided example here on playground. https://play.golang.org/p/4zQsaz5Z_5P

huangapple
  • 本文由 发表于 2021年7月19日 16:24:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/68437317.html
匿名

发表评论

匿名网友

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

确定