戈兰解析具有相同名称的嵌套节点的 XML?

我需要解析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>

如何获取所有索赔文本中的“索赔”数组?我正在尝试这个,但它没有给出索赔文本中包含的任何文本。


type Result struct {

Claims  []Claim `xml:"claims>claim"`

}

type Claim struct{

  ClaimText []string `xml:"claim-text"` 

}

任何帮助将不胜感激。


紫衣仙女
浏览 99回答 2
2回答

慕田峪4524236

type Result struct {&nbsp; &nbsp; Claims []Claim `xml:"claim"`}type Claim struct {&nbsp; &nbsp; ClaimText []ClaimText `xml:"claim-text"`}type ClaimText struct {&nbsp; &nbsp; Value&nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; `xml:",chardata"`&nbsp; &nbsp; ClaimText []ClaimText `xml:"claim-text"`}https://play.golang.org/p/uueAiwG84LH如果你想摆脱空白,你可以实现解封接口:func (t *ClaimText) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {&nbsp; &nbsp; type T ClaimText&nbsp; &nbsp; if err := d.DecodeElement((*T)(t), &start); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; t.Value = strings.TrimSpace(t.Value)&nbsp; &nbsp; return nil}https://play.golang.org/p/2I1meeBm0pu

小怪兽爱吃肉

看看这个在线工具,它生成以下结构:type Claims struct {&nbsp; &nbsp; XMLName xml.Name `xml:"claims"`&nbsp; &nbsp; Text&nbsp; &nbsp; string&nbsp; &nbsp;`xml:",chardata"`&nbsp; &nbsp; Claim&nbsp; &nbsp;[]struct {&nbsp; &nbsp; &nbsp; &nbsp; Text&nbsp; &nbsp; &nbsp; string `xml:",chardata"`&nbsp; &nbsp; &nbsp; &nbsp; ClaimText struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text&nbsp; &nbsp; &nbsp; string `xml:",chardata"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ClaimText []struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text&nbsp; &nbsp; &nbsp; string&nbsp; &nbsp;`xml:",chardata"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ClaimText []string `xml:"claim-text"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } `xml:"claim-text"`&nbsp; &nbsp; &nbsp; &nbsp; } `xml:"claim-text"`&nbsp; &nbsp; } `xml:"claim"`}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go