如何解组动态 XML 数据以进行结构化?

我有一个 XML 数据,其中包含一些具有动态元素名称的重复数据,如下所示。


<Ht>

<Criteria>

    <ADNode_1>2</ADNode_1>

    <CDNode_1>2</CDNode_1>

    <IFNode_1>0</IFNode_1>

    <ADNode_2>2</ADNode_2>

    <CDNode_2>0</CDNode_2>

    <IFNode_2>0</IFNode_2>

    <ADNode_3>0</ADNode_3>

    <CDNode_3>0</CDNode_3>

    <IFNode_3>0</IFNode_3>

</Criteria>

<session id="1056134770841202228344907">

    <Htd ID="21170">

        <Data_1>

            <Info Count="2"></Info>

            <Data Id="iV29308/B2/R1">

                <Type>TR1</Type>

            </Data>

            <Data Id="iV29308/B3/R1">

                <Type>TR1</Type>

            </Data>

            <Data Id="iV29308/B4/R1">

                <Type>TR1</Type>

            </Data>

            <Data Id="iV29308/B6/R1">

                <Type>TR1</Type>

            </Data>

        </Data_1>

        <Data_2>

            <Info Count="2"></Info>

            <Data Id="iV29308/B2/R1">

                <Type>TR2</Type>

            </Data>

            <Data Id="iV29308/B3/R1">

                <Type>TR2</Type>

            </Data>

            <Data Id="iV29308/B4/R1">

                <Type>TR2</Type>

            </Data>

            <Data Id="iV29308/B6/R1">

                <Type>TR3</Type>

            </Data>

        </Data_2>

    </Htd>

</session>

我可以为<ADNode_1>、<ADNode_2>和<ADNode_3>或创建单独的结构<Data_1>,<Data_2>但是可以有 n 个这样的节点。像下面一样


<ADNode_1>2</ADNode_1>

<ADNode_2>2</ADNode_2>

<ADNode_3>2</ADNode_3>

<ADNode_n>2</ADNode_n>

要么


<Data_1></Data_1>

<Data_2></Data_2>

<Data_3></Data_3>

<Data_n></Data_n>

如何为这些节点创建具有 n 个节点或元素的结构?


这是我正在尝试使用的游乐场链接。


RISEBY
浏览 174回答 1
1回答

繁花不似锦

通常对于这些情况,您可以在 Go 中使用切片来“收集”元素,并使用,any选项将您没有映射的所有内容放入其中。为了能够识别来源,请使用一个XMLName xml.Name字段,该字段将保留它源自的 XML 标记的名称。例如,您可以像这样对 XML 进行建模:type Ht struct {&nbsp; &nbsp; Criteria struct {&nbsp; &nbsp; &nbsp; &nbsp; Nodes []struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XMLName xml.Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Content string `xml:",chardata"`&nbsp; &nbsp; &nbsp; &nbsp; } `xml:",any"`&nbsp; &nbsp; }&nbsp; &nbsp; Session struct {&nbsp; &nbsp; &nbsp; &nbsp; ID&nbsp; string `xml:"id,attr"`&nbsp; &nbsp; &nbsp; &nbsp; Htd struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ID&nbsp; &nbsp; string `xml:"ID,attr"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataX []struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XMLName xml.Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Info&nbsp; &nbsp; struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Count int `xml:"Count,attr"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataNodes []struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XMLName xml.Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ID&nbsp; &nbsp; &nbsp; string `xml:"Id,attr"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type&nbsp; &nbsp; string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } `xml:",any"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } `xml:",any"`&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } `xml:"session"`}解析它并重新编码它将保留所有元素:var ht Htif err := xml.Unmarshal([]byte(src), &ht); err != nil {&nbsp; &nbsp; panic(err)}result, err := xml.MarshalIndent(ht, "", "&nbsp; ")if err != nil {&nbsp; &nbsp; panic(err)}fmt.Println(string(result))这将输出(在Go Playground上尝试):<Ht>&nbsp; <Criteria>&nbsp; &nbsp; <ADNode_1>2</ADNode_1>&nbsp; &nbsp; <CDNode_1>2</CDNode_1>&nbsp; &nbsp; <IFNode_1>0</IFNode_1>&nbsp; &nbsp; <ADNode_2>2</ADNode_2>&nbsp; &nbsp; <CDNode_2>0</CDNode_2>&nbsp; &nbsp; <IFNode_2>0</IFNode_2>&nbsp; &nbsp; <ADNode_3>0</ADNode_3>&nbsp; &nbsp; <CDNode_3>0</CDNode_3>&nbsp; &nbsp; <IFNode_3>0</IFNode_3>&nbsp; </Criteria>&nbsp; <session id="1056134770841202228344907">&nbsp; &nbsp; <Htd ID="21170">&nbsp; &nbsp; &nbsp; <Data_1>&nbsp; &nbsp; &nbsp; &nbsp; <Info Count="2"></Info>&nbsp; &nbsp; &nbsp; &nbsp; <Data Id="iV29308/B2/R1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Type>TR1</Type>&nbsp; &nbsp; &nbsp; &nbsp; </Data>&nbsp; &nbsp; &nbsp; &nbsp; <Data Id="iV29308/B3/R1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Type>TR1</Type>&nbsp; &nbsp; &nbsp; &nbsp; </Data>&nbsp; &nbsp; &nbsp; &nbsp; <Data Id="iV29308/B4/R1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Type>TR1</Type>&nbsp; &nbsp; &nbsp; &nbsp; </Data>&nbsp; &nbsp; &nbsp; &nbsp; <Data Id="iV29308/B6/R1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Type>TR1</Type>&nbsp; &nbsp; &nbsp; &nbsp; </Data>&nbsp; &nbsp; &nbsp; </Data_1>&nbsp; &nbsp; &nbsp; <Data_2>&nbsp; &nbsp; &nbsp; &nbsp; <Info Count="2"></Info>&nbsp; &nbsp; &nbsp; &nbsp; <Data Id="iV29308/B2/R1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Type>TR2</Type>&nbsp; &nbsp; &nbsp; &nbsp; </Data>&nbsp; &nbsp; &nbsp; &nbsp; <Data Id="iV29308/B3/R1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Type>TR2</Type>&nbsp; &nbsp; &nbsp; &nbsp; </Data>&nbsp; &nbsp; &nbsp; &nbsp; <Data Id="iV29308/B4/R1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Type>TR2</Type>&nbsp; &nbsp; &nbsp; &nbsp; </Data>&nbsp; &nbsp; &nbsp; &nbsp; <Data Id="iV29308/B6/R1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Type>TR3</Type>&nbsp; &nbsp; &nbsp; &nbsp; </Data>&nbsp; &nbsp; &nbsp; </Data_2>&nbsp; &nbsp; </Htd>&nbsp; </session></Ht>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go