如何解析嵌套的XML?

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

How to unmarshall nested XML?

问题

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

<configuration version="37">
  <folder id="0usj3" label="aaa" type="sendreceive">
    <device id="U34L32N"></device>
    <device id="U34L32NXX"></device>
    <device id="U34L32NYY"></device>
  </folder>
  <folder id="0usj4" label="bbb" type="sendreceive">
    <device id="U34L32NYY"></device>
  </folder>
  <device id="U34L32N" name="wazaa"></device>
  <device id="FJP7437" name="wazii"></device>
</configuration>

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

package main

import (
	"encoding/xml"
	"fmt"
	"io"
	"os"

	"github.com/rs/zerolog/log"
)

type Device struct {
	ID   string `xml:"id,attr"`
	Name string `xml:"name,attr"`
}

type Folder struct {
	ID     string `xml:"id,attr"`
	Label  string `xml:"label,attr"`
	Type   string `xml:"type,attr"`
	Device []Device
}

type Configuration struct {
	Folder []Folder `xml:"folder"`
	Device []Device `xml:"device"`
}

func main() {
	var err error

	xmlFile, errOpen := os.Open("config-test.xml")
	byteValue, errRead := io.ReadAll(xmlFile)
	if errOpen != nil || errRead != nil {
		log.Fatal().Msgf("无法打开%v或读取%vconfig.xml%v", errOpen, errRead)
	}
	defer xmlFile.Close()

	config := Configuration{}
	err = xml.Unmarshal(byteValue, &config)
	if err != nil {
		log.Fatal().Msgf("无法解组XML%v", err)
	}

	fmt.Printf("%v", config)
}

我得到了一个部分结果:

{[{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:

<configuration version="37">
  <folder id="0usj3" label="aaa" type="sendreceive">
    <device id="U34L32N"></device>
    <device id="U34L32NXX"></device>
    <device id="U34L32NYY"></device>
  </folder>
  <folder id="0usj4" label="bbb" type="sendreceive">
    <device id="U34L32NYY"></device>
  </folder>
  <device id="U34L32N" name="wazaa"></device>
  <device id="FJP7437" name="wazii"></device>
</configuration>

This is the code I am using for that:

package main

import (
	"encoding/xml"
	"fmt"
	"io"
	"os"

	"github.com/rs/zerolog/log"
)

type Device struct {
	ID   string `xml:"id,attr"`
	Name string `xml:"name,attr"`
}

type Folder struct {
	ID     string `xml:"id,attr"`
	Label  string `xml:"label,attr"`
	Type   string `xml:"type,attr"`
	Device []Device
}

type Configuration struct {
	Folder []Folder `xml:"folder"`
	Device []Device `xml:"device"`
}

func main() {
	var err error

	xmlFile, errOpen := os.Open("config-test.xml")
	byteValue, errRead := io.ReadAll(xmlFile)
	if errOpen != nil || errRead != nil {
		log.Fatal().Msgf("cannot open (%v) or read (%v) config.xml: %v", errOpen, errRead)
	}
	defer xmlFile.Close()

	config := Configuration{}
	err = xml.Unmarshal(byteValue, &config)
	if err != nil {
		log.Fatal().Msgf("cannot unmarshall XML: %v", err)
	}

	fmt.Printf("%v", config)
}

I get a partial result:

{[{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缺少注释。

type Folder struct {
	ID     string   `xml:"id,attr"`
	Label  string   `xml:"label,attr"`
	Type   string   `xml:"type,attr"`
	Device []Device `xml:"device"`
}
英文:

The annotation is missing on Device in Folder.

type Folder struct {
	ID     string   `xml:"id,attr"`
	Label  string   `xml:"label,attr"`
	Type   string   `xml:"type,attr"`
	Device []Device `xml:"device"`
}

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:

确定