我有一个平面 XML 结构,我试图将其解组为一个 go 数据结构。我试图找到一种方法从下面的 XML ie- bucket1 = [apple,orange,grapes], bucket2= [apple,mangoes] 中获取每个桶中的项目列表(项目名称)。
当我尝试将 xml 解组为下面的 go 数据结构时,我能够获得桶名和项目的列表,但我无法将项目列表映射到它们各自的桶,因为每个桶可以有很多项目。有没有办法通过更改go数据结构来从这个xml中实现这个需求?我无法控制 XML 的结构,因此无法更改它以满足我的要求。我是新来的,我很感激这里的任何意见。
type buckets struct {
XMLName xml.Name `xml:"buckets"`
BucketName []string `xml:"bucket-name"`
ItemName []string `xml:"item-name"`
Weight []string `xml:"weight"`
Quantity []string `xml:"quantity"`
}
<?xml version="1.0" encoding="UTF-8"?>
<buckets>
<bucket-name>bucket1</bucket-name>
<item-name>apple</item-name>
<weight>500</weight>
<quantity>3</quantity>
<item-name>orange</item-name>
<weight>500</weight>
<quantity>2</quantity>
<item-name>grapes</item-name>
<weight>800</weight>
<quantity>1</quantity>
<bucket-name>bucket2</bucket-name>
<item-name>apple</item-name>
<weight>500</weight>
<quantity>3</quantity>
<item-name>mangoes</item-name>
<weight>400</weight>
<quantity>2</quantity>
</buckets>
有只小跳蛙
蝴蝶刀刀
相关分类