dqm4675
dqm4675
2019-09-09 12:43
浏览 158

解析动态XML

I understand how to unmarshal simple xml data to Go structs but can't figure out how to handle dynamic tags. Here's an example. There can be <image_3><image_4> etc

<?xml version="1.0" encoding="utf-8"?>
<products>
  <product>
    <product_id>11600</product_id>
    <date_created><![CDATA[2018-10-19 15:20:22]]></date_created>
    <price>200</price>
    <stock_status>In Stock</stock_status>
    <images>
      <image_1>1.jpg</image_1>
      <image_2>2.jpg</image_2>
   </images
   </product>
</products>

//update

type Products struct {
        XMLName xml.Name `xml:"products"`
        Text    string   `xml:",chardata"`
        Product struct {
                Text        string `xml:",chardata"`
                ProductID   string `xml:"product_id"`
                DateCreated string `xml:"date_created"`
                Price       string `xml:"price"`
                StockStatus string `xml:"stock_status"`
                Images          map[string]string `xml:"images"`
        } `xml:"product"`
} 

When I run fmt.Println(len(products.Product[0].Images)) I get 0. What I'm missing here?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxie0824
    douxie0824 2019-09-10 08:07
    已采纳

    You can implement the xml.Unmarshaler interface on a custom map type like so:

    type Images map[string]string
    
    func (i *Images) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
        *i = make(Images) // initialize the map
        for {
            tok, err := d.Token()
            if err != nil {
                if err == io.EOF {
                    return nil
                }
                return err
            }
    
            if se, ok := tok.(xml.StartElement); ok {
                tok, err = d.Token()
                if err != nil {
                    if err == io.EOF {
                        return nil
                    }
                    return err
                }
                if data, ok := tok.(xml.CharData); ok {
                    (*i)[se.Name.Local] = string(data)
                }
            }
        }
    }
    

    https://play.golang.com/p/gi9Fiv3PS8M

    点赞 评论
  • duanjian7617
    duanjian7617 2019-09-09 14:18

    You can use this library (https://github.com/clbanning/mxj):

    var input = []byte(`<?xml version="1.0" encoding="utf-8"?>
        <products>
            <product>
                <name>hello</name>
                <images>
                    <image1>one</image1>
                    <image2>one</image2>
                </images>
            </product>
        </products>`)
    
    type Products struct {
        XMLName xml.Name `xml:"products"`
        Products []Product `xml:"product"`
    }
    
    type Product struct {
        XMLName xml.Name `xml:"product"`
        Name string `xml:"name"`
        Images map[string]interface{}
    }
    
    
    func (p *Product) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    
        var v struct {
            XMLName xml.Name `xml:"product"`
            Name string `xml:"name"`
            Images struct {
                Inner []byte `xml:",innerxml"`
            } `xml:"images"`
        }
    
        err := d.DecodeElement(&v, &start)
        if err != nil {
            return err
        }
    
        images := make(map[string]interface{})
        // use mxj here and fill images from v.Images.Inner
    
        p.Name = v.Name
        p.Images = images
    
        return nil
    }
    
    
    func main() {
    
        var data Products
        err := xml.Unmarshal(input, &data)
        if err != nil {
            panic(err)
        }
    }
    
    点赞 评论

相关推荐