鍍金池/ 問(wèn)答/GO/ 這樣的xml怎么定義結(jié)構(gòu)體?

這樣的xml怎么定義結(jié)構(gòu)體?

clipboard.png

要生成如圖格式的xml,怎么定義結(jié)構(gòu)體呢?我定義的如下,但是有問(wèn)題,不知道哪出了問(wèn)題,有大神指導(dǎo)一下嗎?

type Images struct {
        Img   string `xml:"img"`
        Index int    `xml:"index,attr"`
    }
type Data struct {
    OuterID string  `xml:"outerID"`
        Name    string  `xml:"name"`
        Price   float64 `xml:"price"`
        Value   float64 `xml:"value"`
        // Saving           float64      `xml:"saving"`
        PriceUnit        string    `xml:"priceUnit"`
        Availability     int       `xml:"availability"`
        Image            string    `xml:"image"`
        Brand            string    `xml:"brand"`
        Loc              string    `xml:"loc"`
        PcLoc            string    `xml:"pcLoc"`
        SellerSiteUrl    string    `xml:"sellerSiteUrl"`
        ShopName         string    `xml:"shopName"`
        SearchWiseUrl    string    `xml:"searchWiseUrl"`
        Category         string    `xml:"category"`
        CategoryUrl      string    `xml:"categoryUrl"`
        CategoryPcUrl    string    `xml:"categoryPcUrl"`
        SubCategory      string    `xml:"subCategory"`
        SubCategoryUrl   string    `xml:"subCategoryUrl"`
        SubcategoryPcUrl string    `xml:"subcategoryPcUrl"`
        SellerName       string    `xml:"sellerName"`
        Logo             string    `xml:"logo"`
        MoreImages       []Images  `xml:"moreImages"`
        Choice           Attribute `xml:"choice"`        
}

生成xml圖片的代碼片段如下:

                            _imgs := getImages(outerID)
                            if len(_imgs) <= 0 {
                                continue
                            }
                            _firstImg := ""
                            var _moreImages []Images
                            for i, v := range _imgs {
                                if i == 0 {
                                    _firstImg = v
                                } else {
                                    _moreImages = append(_moreImages, Images{Img: v, Index: i})
                                }
                            }
                            // fmt.Println(moreImages)
                            // os.Exit(1)
                            moreImages := _moreImages
                            subAttribute := []SubAttribute{SubAttribute{Key: "ext_down_load", Value: "https://m.mia.com/detail-a-" + outerID + ".html"}, SubAttribute{Key: "ext_put_url", Value: "https://m.mia.com/detail-a-" + outerID + ".html"}}
                            //Data數(shù)據(jù)
                            _choice := Attribute{Attribute: subAttribute}
                            itemData := Data{
                                OuterID:          outerID,
                                Name:             name,
                                Price:            price,
                                Value:            value,
                                PriceUnit:        _PRICE_UNIT,
                                Availability:     _AVAILABILITY,
                                Image:            _firstImg,
                                MoreImages:       moreImages,
                                Brand:            brand,
                                Loc:              _loc,
                                PcLoc:            getPcLoc(outerID),
                                SellerSiteUrl:    _SELLER_SITE_URL,
                                ShopName:         _SHOP_NAME,
                                SearchWiseUrl:    getSearchWiseUrl(outerID),
                                Category:         category_id,
                                CategoryUrl:      getCategoryUrl(category_id),
                                CategoryPcUrl:    getCategoryPcUrl(category_id),
                                SubCategory:      _categoryInfo["subCategoryId"],
                                SubCategoryUrl:   getCategoryUrl(_categoryInfo["subCategoryId"]),
                                SubcategoryPcUrl: getCategoryPcUrl(_categoryInfo["subCategoryId"]),
                                Choice:           _choice,
                                SellerName:       _SELLER_NAME,
                                Logo:             _LOGO}

生產(chǎn)xml的格式是這樣的:
clipboard.png

請(qǐng)問(wèn)上述代碼哪出了問(wèn)題呢?

回答
編輯回答
黑與白
type Images struct {
        //內(nèi)部屬性都要大寫(xiě)字母開(kāi)頭,屬性節(jié)點(diǎn)的名稱變量名固定為XMLName,內(nèi)部的文本統(tǒng)一叫innerxml
        Index     int    `xml:"index,attr"` //表示屬性
        InnerText string `xml:",innerxml"`  //表示文本
    }
    type Mimages struct {
        XMLName xml.Name `xml:"moreImages"`
        Img     []Images `xml:"img"`
    }

片段:

var _moreImages []Images
                        for i, v := range _imgs {
                            if i == 0 {
                                _firstImg = v
                            } else {
                                _moreImages = append(_moreImages, Images{InnerText: v, Index: i})
                            }
                        }
                        moreImages := Mimages{Img: _moreImages}
2018年7月19日 16:04