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

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

Golang parse XML with nested nodes of the same name?

问题

我需要解析 XML 代码,

<claims>
    <claim>
        <claim-text>ABC
            <claim-text>PQR</claim-text>
            <claim-text>Xyz
                <claim-text>A</claim-text>
                <claim-text>B</claim-text>
                <claim-text>C</claim-text>
            </claim-text>
        </claim-text>
    </claim>
    <claim>
        <claim-text>PPP
            <claim-text>ZZZ</claim-text>
            <claim-text>MMM</claim-text>
        </claim-text>
    </claim>
</claims>

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

type Result struct {
    Claims  []Claim `xml:"claims>claim"`
}
type Claim struct{
    ClaimText []string `xml:"claim-text"`    
}

任何帮助将不胜感激。

英文:

I need to parse xml code,

&lt;claims&gt;
	&lt;claim&gt;
		&lt;claim-text&gt;ABC
			&lt;claim-text&gt;PQR&lt;/claim-text&gt;
			&lt;claim-text&gt;Xyz
				&lt;claim-text&gt;A&lt;/claim-text&gt;
				&lt;claim-text&gt;B&lt;/claim-text&gt;
				&lt;claim-text&gt;C&lt;/claim-text&gt;
			&lt;/claim-text&gt;
		&lt;/claim-text&gt;
	&lt;/claim&gt;
	&lt;claim&gt;
		&lt;claim-text&gt;PPP
			&lt;claim-text&gt;ZZZ&lt;/claim-text&gt;
			&lt;claim-text&gt;MMM&lt;/claim-text&gt;
		&lt;/claim-text&gt;
	&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.

type Result struct {
Claims  []Claim `xml:&quot;claims&gt;claim&quot;`
}
type Claim struct{
  ClaimText []string `xml:&quot;claim-text&quot;`	
}

Any help would be appreciated.

答案1

得分: 2

type Result struct {
	Claims []Claim `xml:"claim"`
}

type Claim struct {
	ClaimText []ClaimText `xml:"claim-text"`
}

type ClaimText struct {
	Value     string      `xml:",chardata"`
	ClaimText []ClaimText `xml:"claim-text"`
}

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

func (t *ClaimText) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	type T ClaimText
	if err := d.DecodeElement((*T)(t), &start); err != nil {
		return err
	}

	t.Value = strings.TrimSpace(t.Value)
	return nil
}

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


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

func (t *ClaimText) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	type T ClaimText
	if err := d.DecodeElement((*T)(t), &start); err != nil {
		return err
	}

	t.Value = strings.TrimSpace(t.Value)
	return nil
}

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

英文:
type Result struct {
	Claims []Claim `xml:&quot;claim&quot;`
}

type Claim struct {
	ClaimText []ClaimText `xml:&quot;claim-text&quot;`
}

type ClaimText struct {
	Value     string      `xml:&quot;,chardata&quot;`
	ClaimText []ClaimText `xml:&quot;claim-text&quot;`
}

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


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

func (t *ClaimText) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	type T ClaimText
	if err := d.DecodeElement((*T)(t), &amp;start); err != nil {
		return err
	}

	t.Value = strings.TrimSpace(t.Value)
	return nil
}

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

答案2

得分: 1

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

type Claims struct {
	XMLName xml.Name `xml:"claims"`
	Text    string   `xml:",chardata"`
	Claim   []struct {
		Text      string `xml:",chardata"`
		ClaimText struct {
			Text      string `xml:",chardata"`
			ClaimText []struct {
				Text      string   `xml:",chardata"`
				ClaimText []string `xml:"claim-text"`
			} `xml:"claim-text"`
		} `xml:"claim-text"`
	} `xml:"claim"`
}
英文:

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

type Claims struct {
	XMLName xml.Name `xml:&quot;claims&quot;`
	Text    string   `xml:&quot;,chardata&quot;`
	Claim   []struct {
		Text      string `xml:&quot;,chardata&quot;`
		ClaimText struct {
			Text      string `xml:&quot;,chardata&quot;`
			ClaimText []struct {
				Text      string   `xml:&quot;,chardata&quot;`
				ClaimText []string `xml:&quot;claim-text&quot;`
			} `xml:&quot;claim-text&quot;`
		} `xml:&quot;claim-text&quot;`
	} `xml:&quot;claim&quot;`
} 

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:

确定