英文:
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)或读取(%v)config.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"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论