亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

XML中的結構數組沒有包裝節點?

XML中的結構數組沒有包裝節點?

Go
慕婉清6462132 2022-10-10 19:21:01
我有一個客戶擁有 XML,他們似乎想要一個庫存數組,但是每個項目一個接一個地列出,沒有包裝節點。這是我正在做的一個示例,但每個項目都有一個包裝節點。有沒有辦法讓它們在“root”下一個接一個地列出?測試代碼:package mainimport (    "encoding/xml"    "fmt"    "os"    "strconv")func main() {    type InventoryItem struct {        XMLName  xml.Name        ItemName string `xml:"Name"`        ItemDescription string `xml:"Description"`    }    type XMLEnvelop struct {        XMLName   xml.Name        `xml:"root"`        Inventory []InventoryItem `xml:"item"`        Records   int             `xml:"records"`    }    var items []InventoryItem    for i := 1; i < 6; i++ {        items = append(items, InventoryItem{XMLName: xml.Name{Local: "item" + strconv.Itoa(i)}, ItemName: "Test " + strconv.Itoa(i), ItemDescription: "Description " + strconv.Itoa(i)})    }    v := &XMLEnvelop{Records: 1, Inventory: items}    output, err := xml.MarshalIndent(v, "", "    ")    if err != nil {        fmt.Printf("error: %v\n", err)    }    // Write the output to check    os.Stdout.Write(output)    //Here is where I would make the request}測試輸出:<root>    <item1>        <Name>Test 1</Name>        <Description>Description 1</Description>    </item1>    <item2>        <Name>Test 2</Name>        <Description>Description 2</Description>    </item2>    <item3>        <Name>Test 3</Name>        <Description>Description 3</Description>    </item3>    <item4>        <Name>Test 4</Name>        <Description>Description 4</Description>    </item4>    <item5>        <Name>Test 5</Name>        <Description>Description 5</Description>    </item5>    <records>1</records></root>去游樂場: https: //play.golang.org/p/3DRUABFEQvC這是他們似乎正在尋找的輸出......無論出于何種原因。<root>    <Name>Test 1</Name>    <Description>Description 1</Description>    <Name>Test 2</Name>    <Description>Description 2</Description>    <Name>Test 3</Name>    <Description>Description 3</Description>    <Name>Test 4</Name>    <Description>Description 4</Description>    <Name>Test 5</Name>    <Description>Description 5</Description>    <records>1</records></root>
查看完整描述

1 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

您可以實現自定義封送拆收器。

type InventoryItem struct {

    XMLName         xml.Name

    ItemName        string `xml:"Name"`

    ItemDescription string `xml:"Description"`

}


func (i InventoryItem) MarshalXML(e *xml.Encoder, start xml.StartElement) error {

    // Ignore the passed in `start` argument, it represents the

    // parent InventoryItem element of the Name and Description.


    // Declare types to represent the elements you want to encode,

    // initialize them to the item's field values and encode them.

    //

    // Use the ",chardata" option to tell the encoder to encode the

    // field's value directly rather than as a child element. 

    type Name struct {

        Value string `xml:",chardata"`

    }

    if err := e.Encode(Name{i.ItemName}); err != nil {

        return err

    }

    type Description struct {

        Value string `xml:",chardata"`

    }

    return e.Encode(Description{i.ItemDescription})

}

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


查看完整回答
反對 回復 2022-10-10
  • 1 回答
  • 0 關注
  • 99 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號