使用Golang解析具有相同名称的嵌套节点的XML?

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

Golang parse XML with nested nodes of the same name?

问题

我需要解析 XML 代码,

  1. <claims>
  2. <claim>
  3. <claim-text>ABC
  4. <claim-text>PQR</claim-text>
  5. <claim-text>Xyz
  6. <claim-text>A</claim-text>
  7. <claim-text>B</claim-text>
  8. <claim-text>C</claim-text>
  9. </claim-text>
  10. </claim-text>
  11. </claim>
  12. <claim>
  13. <claim-text>PPP
  14. <claim-text>ZZZ</claim-text>
  15. <claim-text>MMM</claim-text>
  16. </claim-text>
  17. </claim>
  18. </claims>

如何获取包含所有声明文本的 'claim' 数组?
我尝试了以下代码,但它没有返回 claim-text 中的任何文本。

  1. type Result struct {
  2. Claims []Claim `xml:"claims>claim"`
  3. }
  4. type Claim struct{
  5. ClaimText []string `xml:"claim-text"`
  6. }

任何帮助将不胜感激。

英文:

I need to parse xml code,

  1. &lt;claims&gt;
  2. &lt;claim&gt;
  3. &lt;claim-text&gt;ABC
  4. &lt;claim-text&gt;PQR&lt;/claim-text&gt;
  5. &lt;claim-text&gt;Xyz
  6. &lt;claim-text&gt;A&lt;/claim-text&gt;
  7. &lt;claim-text&gt;B&lt;/claim-text&gt;
  8. &lt;claim-text&gt;C&lt;/claim-text&gt;
  9. &lt;/claim-text&gt;
  10. &lt;/claim-text&gt;
  11. &lt;/claim&gt;
  12. &lt;claim&gt;
  13. &lt;claim-text&gt;PPP
  14. &lt;claim-text&gt;ZZZ&lt;/claim-text&gt;
  15. &lt;claim-text&gt;MMM&lt;/claim-text&gt;
  16. &lt;/claim-text&gt;
  17. &lt;/claim&gt;

</claims>

How to get array of 'claim' with inside all claim texts?
I was trying this but it does not give whatever text enclosed in claim-text.

  1. type Result struct {
  2. Claims []Claim `xml:&quot;claims&gt;claim&quot;`
  3. }
  4. type Claim struct{
  5. ClaimText []string `xml:&quot;claim-text&quot;`
  6. }

Any help would be appreciated.

答案1

得分: 2

  1. type Result struct {
  2. Claims []Claim `xml:"claim"`
  3. }
  4. type Claim struct {
  5. ClaimText []ClaimText `xml:"claim-text"`
  6. }
  7. type ClaimText struct {
  8. Value string `xml:",chardata"`
  9. ClaimText []ClaimText `xml:"claim-text"`
  10. }

如果你想去掉空白字符,你可以实现unmarshaler接口:

  1. func (t *ClaimText) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  2. type T ClaimText
  3. if err := d.DecodeElement((*T)(t), &start); err != nil {
  4. return err
  5. }
  6. t.Value = strings.TrimSpace(t.Value)
  7. return nil
  8. }

链接:https://play.golang.org/p/uueAiwG84LH


如果你想去掉空白字符,你可以实现unmarshaler接口:

  1. func (t *ClaimText) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  2. type T ClaimText
  3. if err := d.DecodeElement((*T)(t), &start); err != nil {
  4. return err
  5. }
  6. t.Value = strings.TrimSpace(t.Value)
  7. return nil
  8. }

链接:https://play.golang.org/p/2I1meeBm0pu

英文:
  1. type Result struct {
  2. Claims []Claim `xml:&quot;claim&quot;`
  3. }
  4. type Claim struct {
  5. ClaimText []ClaimText `xml:&quot;claim-text&quot;`
  6. }
  7. type ClaimText struct {
  8. Value string `xml:&quot;,chardata&quot;`
  9. ClaimText []ClaimText `xml:&quot;claim-text&quot;`
  10. }

https://play.golang.org/p/uueAiwG84LH


If you want to get rid of the white-space, you can implement the unmarshaler interface:

  1. func (t *ClaimText) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  2. type T ClaimText
  3. if err := d.DecodeElement((*T)(t), &amp;start); err != nil {
  4. return err
  5. }
  6. t.Value = strings.TrimSpace(t.Value)
  7. return nil
  8. }

https://play.golang.org/p/2I1meeBm0pu

答案2

得分: 1

请看这个生成以下结构的在线工具

  1. type Claims struct {
  2. XMLName xml.Name `xml:"claims"`
  3. Text string `xml:",chardata"`
  4. Claim []struct {
  5. Text string `xml:",chardata"`
  6. ClaimText struct {
  7. Text string `xml:",chardata"`
  8. ClaimText []struct {
  9. Text string `xml:",chardata"`
  10. ClaimText []string `xml:"claim-text"`
  11. } `xml:"claim-text"`
  12. } `xml:"claim-text"`
  13. } `xml:"claim"`
  14. }
英文:

Take a look at this online tool that generate the following struct:

  1. type Claims struct {
  2. XMLName xml.Name `xml:&quot;claims&quot;`
  3. Text string `xml:&quot;,chardata&quot;`
  4. Claim []struct {
  5. Text string `xml:&quot;,chardata&quot;`
  6. ClaimText struct {
  7. Text string `xml:&quot;,chardata&quot;`
  8. ClaimText []struct {
  9. Text string `xml:&quot;,chardata&quot;`
  10. ClaimText []string `xml:&quot;claim-text&quot;`
  11. } `xml:&quot;claim-text&quot;`
  12. } `xml:&quot;claim-text&quot;`
  13. } `xml:&quot;claim&quot;`
  14. }

huangapple
  • 本文由 发表于 2021年9月7日 17:44:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/69085903.html
匿名

发表评论

匿名网友

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

确定