这样的xml怎么定义结构体?

clipboard.png

要生成如图格式的xml,怎么定义结构体呢?我定义的如下,但是有问题,不知道哪出了问题,有大神指导一下吗?

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数据
                            _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}

生产xml的格式是这样的:
clipboard.png

请问上述代码哪出了问题呢?

阅读 3.5k
1 个回答
type Images struct {
        //内部属性都要大写字母开头,属性节点的名称变量名固定为XMLName,内部的文本统一叫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}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题