Unmarshalling XML using Go: How to find attributes with the same value?

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

Unmarshalling XML using Go: How to find attributes with the same value?

问题

我正在尝试解析下面的XML,如何找到所有<info>节点中type="Genres"的节点,并将它们的值存储在[]Genre中?

<manga id="4199" type="manga" name="Jinki: Extend" precision="manga">
    <info type="Main title" lang="EN">Jinki: Extend</info>
    <info type="Genres">action</info>
    <info type="Genres">science fiction</info>
    <info type="Themes">mecha</info>
    <info type="Number of tankoubon">9</info>
    <info type="Number of pages">186</info>
</manga>

我希望将这些值存储在类似以下结构的结构体中:

// Manga 结构体
type Manga struct {
    WorkID    int     `xml:"id,attr"`
    Name      string  `xml:"name,attr"`
    Precision string  `xml:"precision,attr"`
    Genres    []Genre // 这部分我需要帮助
}

// Genre 结构体
type Genre struct {
    Value string
}

我知道这个XML不是理想的,但这是我必须处理的,希望你们能帮助我。

提前感谢。

英文:

I'm having trouble unmarshalling the XML below, how do I find all &lt;info&gt; nodes with type=&quot;Genres&quot; and store their values inside a []Genre?

&lt;manga id=&quot;4199&quot; type=&quot;manga&quot; name=&quot;Jinki: Extend&quot; precision=&quot;manga&quot;&gt;
    &lt;info type=&quot;Main title&quot; lang=&quot;EN&quot;&gt;Jinki: Extend&lt;/info&gt;
    &lt;info type=&quot;Genres&quot;&gt;action&lt;/info&gt;
    &lt;info type=&quot;Genres&quot;&gt;science fiction&lt;/info&gt;
    &lt;info type=&quot;Themes&quot;&gt;mecha&lt;/info&gt;
    &lt;info type=&quot;Number of tankoubon&quot;&gt;9&lt;/info&gt;
    &lt;info type=&quot;Number of pages&quot;&gt;186&lt;/info&gt;
&lt;/manga&gt;

I'm looking to store the values in structs similar to these:

// Manga struct
type Manga struct {
    WorkID    int     `xml:&quot;id,attr&quot;`
    Name      string  `xml:&quot;name,attr&quot;`
    Precision string  `xml:&quot;precision,attr&quot;`
    Genres    []Genre `[this is the part I need help on]`
}

// Genre struct
type Genre struct {
    Value string
}

I know the XML is not ideal, but it is what I have to work with, I hope you guys can help me with this.

Thanks in advance.

答案1

得分: 3

由于<manga>包含了一系列的<info>元素,因此更合理的做法是使用Info结构体的列表,而不是尝试将<info>元素翻译成各种类型。我会定义如下的数据结构:

type Manga struct {
    WorkID    int    `xml:"id,attr"`
    Name      string `xml:"name,attr"`
    Precision string `xml:"precision,attr"`
    Info      []Info `xml:"info"`
}

type Info struct {
    Type  string `xml:"type,attr"`
    Value string `xml:",chardata"`
}

输出结果(以JSON编码方便查看)如下:

{
  "WorkID": 4199,
  "Name": "Jinki: Extend",
  "Precision": "manga",
  "Info": [
    {
      "Type": "Main title",
      "Value": "Jinki: Extend"
    },
    {
      "Type": "Genres",
      "Value": "action"
    },
    {
      "Type": "Genres",
      "Value": "science fiction"
    },
    {
      "Type": "Themes",
      "Value": "mecha"
    },
    {
      "Type": "Number of tankoubon",
      "Value": "9"
    },
    {
      "Type": "Number of pages",
      "Value": "186"
    }
  ]
}
英文:

Since &lt;manga&gt; contains a list of &lt;info&gt; elements, it makes more sense to have a list of Info structs rather than trying to translate the &lt;info&gt; elements into various types. I would define the data structures like:

type Manga struct {
	WorkID    int    `xml:&quot;id,attr&quot;`
	Name      string `xml:&quot;name,attr&quot;`
	Precision string `xml:&quot;precision,attr&quot;`
	Info      []Info `xml:&quot;info&quot;`
}

type Info struct {
	Type  string `xml:&quot;type,attr&quot;`
	Value string `xml:&quot;,chardata&quot;`
}

The output (json encoded for convenience) looks like:

{
  &quot;WorkID&quot;: 4199,
  &quot;Name&quot;: &quot;Jinki: Extend&quot;,
  &quot;Precision&quot;: &quot;manga&quot;,
  &quot;Info&quot;: [
    {
      &quot;Type&quot;: &quot;Main title&quot;,
      &quot;Value&quot;: &quot;Jinki: Extend&quot;
    },
    {
      &quot;Type&quot;: &quot;Genres&quot;,
      &quot;Value&quot;: &quot;action&quot;
    },
    {
      &quot;Type&quot;: &quot;Genres&quot;,
      &quot;Value&quot;: &quot;science fiction&quot;
    },
    {
      &quot;Type&quot;: &quot;Themes&quot;,
      &quot;Value&quot;: &quot;mecha&quot;
    },
    {
      &quot;Type&quot;: &quot;Number of tankoubon&quot;,
      &quot;Value&quot;: &quot;9&quot;
    },
    {
      &quot;Type&quot;: &quot;Number of pages&quot;,
      &quot;Value&quot;: &quot;186&quot;
    }
  ]
}

答案2

得分: 0

如果XML文件不是很大,我会写一个实用函数,类似于:

type Manga struct {
    WorkID    int    `xml:"id,attr"`
    Name      string `xml:"name,attr"`
    Precision string `xml:"precision,attr"`
    Info      []Info `xml:"info"`
}

type Info struct {
    Type string `xml:"type,attr"`
    Data string `xml:",chardata"`
}

func (m *Manga) Genres() []Info {
    var g []Info

    for _, v := range m.Info {
        if v.Type == "Genres" {
            g = append(g, v)
        }
    }
    return g
}

在这里查看它的运行效果:https://play.golang.org/p/bebUwwbSwG

英文:

In case the XML won't not very large, I would just write an utility function like:

type Manga struct {
	WorkID    int    `xml:&quot;id,attr&quot;`
	Name      string `xml:&quot;name,attr&quot;`
	Precision string `xml:&quot;precision,attr&quot;`
	Info      []Info `xml:&quot;info&quot;`
}

type Info struct {
	Type string `xml:&quot;type,attr&quot;`
	Data string `xml:&quot;,chardata&quot;`
}

func (m *Manga) Genres() []Info {
	var g []Info

	for _, v := range m.Info {
		if v.Type == &quot;Genres&quot; {
			g = append(g, v)
		}
	}
	return g

}

See it in action: https://play.golang.org/p/bebUwwbSwG

huangapple
  • 本文由 发表于 2016年8月5日 22:24:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/38791598.html
匿名

发表评论

匿名网友

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

确定