如何解析嵌套的XML?

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

How to unmarshall nested XML?

问题

我有一个名为config-test.xml的XML文件,我想要进行解组:

  1. <configuration version="37">
  2. <folder id="0usj3" label="aaa" type="sendreceive">
  3. <device id="U34L32N"></device>
  4. <device id="U34L32NXX"></device>
  5. <device id="U34L32NYY"></device>
  6. </folder>
  7. <folder id="0usj4" label="bbb" type="sendreceive">
  8. <device id="U34L32NYY"></device>
  9. </folder>
  10. <device id="U34L32N" name="wazaa"></device>
  11. <device id="FJP7437" name="wazii"></device>
  12. </configuration>

我正在使用以下代码进行解组:

  1. package main
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "io"
  6. "os"
  7. "github.com/rs/zerolog/log"
  8. )
  9. type Device struct {
  10. ID string `xml:"id,attr"`
  11. Name string `xml:"name,attr"`
  12. }
  13. type Folder struct {
  14. ID string `xml:"id,attr"`
  15. Label string `xml:"label,attr"`
  16. Type string `xml:"type,attr"`
  17. Device []Device
  18. }
  19. type Configuration struct {
  20. Folder []Folder `xml:"folder"`
  21. Device []Device `xml:"device"`
  22. }
  23. func main() {
  24. var err error
  25. xmlFile, errOpen := os.Open("config-test.xml")
  26. byteValue, errRead := io.ReadAll(xmlFile)
  27. if errOpen != nil || errRead != nil {
  28. log.Fatal().Msgf("无法打开%v或读取%vconfig.xml%v", errOpen, errRead)
  29. }
  30. defer xmlFile.Close()
  31. config := Configuration{}
  32. err = xml.Unmarshal(byteValue, &config)
  33. if err != nil {
  34. log.Fatal().Msgf("无法解组XML%v", err)
  35. }
  36. fmt.Printf("%v", config)
  37. }

我得到了一个部分结果:

  1. {[{0usj3 aaa sendreceive []} {0usj4 bbb sendreceive []}] [{U34L32N wazaa} {FJP7437 wazii}]}

这是部分结果,因为XML被正确解析,但嵌套在<folder>中的<device>条目没有被考虑进去。

是否有特殊的方式来声明这些嵌套元素?

英文:

I have the following XML file (config-test.xml) that I would to unmarshall:

  1. <configuration version="37">
  2. <folder id="0usj3" label="aaa" type="sendreceive">
  3. <device id="U34L32N"></device>
  4. <device id="U34L32NXX"></device>
  5. <device id="U34L32NYY"></device>
  6. </folder>
  7. <folder id="0usj4" label="bbb" type="sendreceive">
  8. <device id="U34L32NYY"></device>
  9. </folder>
  10. <device id="U34L32N" name="wazaa"></device>
  11. <device id="FJP7437" name="wazii"></device>
  12. </configuration>

This is the code I am using for that:

  1. package main
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "io"
  6. "os"
  7. "github.com/rs/zerolog/log"
  8. )
  9. type Device struct {
  10. ID string `xml:"id,attr"`
  11. Name string `xml:"name,attr"`
  12. }
  13. type Folder struct {
  14. ID string `xml:"id,attr"`
  15. Label string `xml:"label,attr"`
  16. Type string `xml:"type,attr"`
  17. Device []Device
  18. }
  19. type Configuration struct {
  20. Folder []Folder `xml:"folder"`
  21. Device []Device `xml:"device"`
  22. }
  23. func main() {
  24. var err error
  25. xmlFile, errOpen := os.Open("config-test.xml")
  26. byteValue, errRead := io.ReadAll(xmlFile)
  27. if errOpen != nil || errRead != nil {
  28. log.Fatal().Msgf("cannot open (%v) or read (%v) config.xml: %v", errOpen, errRead)
  29. }
  30. defer xmlFile.Close()
  31. config := Configuration{}
  32. err = xml.Unmarshal(byteValue, &config)
  33. if err != nil {
  34. log.Fatal().Msgf("cannot unmarshall XML: %v", err)
  35. }
  36. fmt.Printf("%v", config)
  37. }

I get a partial result:

  1. {[{0usj3 aaa sendreceive []} {0usj4 bbb sendreceive []}] [{U34L32N wazaa} {FJP7437 wazii}]}

It is partial because the XML was parsed correctly, but the <device> entries nested in <folder> were not accounted for.

Is there a special way I should decalre these nested elements?

答案1

得分: 2

Folder中的Device缺少注释。

  1. type Folder struct {
  2. ID string `xml:"id,attr"`
  3. Label string `xml:"label,attr"`
  4. Type string `xml:"type,attr"`
  5. Device []Device `xml:"device"`
  6. }
英文:

The annotation is missing on Device in Folder.

  1. type Folder struct {
  2. ID string `xml:"id,attr"`
  3. Label string `xml:"label,attr"`
  4. Type string `xml:"type,attr"`
  5. Device []Device `xml:"device"`
  6. }

huangapple
  • 本文由 发表于 2023年1月5日 00:55:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75008717.html
匿名

发表评论

匿名网友

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

确定