dspows0637 2015-05-11 16:37
浏览 30
已采纳

Golang Xml解组,无价值

type VastHtml struct {
     VAST struct {
             Version string `xml:"version,attr"`
             Ad      struct {
                     Id     string `xml:"id,attr"`
                     InLine struct {
                             AdSystem    string   `xml:"AdSystem"`
                             AdTitle     string   `xml:"AdTitle"`
                             Description string   `xml:"Description"`
                             Error       string   `xml:"Error"`
                             Impression  []string `xml:"Impression"`
                             Creatives   struct {
                                     Creative []struct {
                                             Sequence string `xml:"sequence,attr"`
                                             Id       string `xml:"id,attr"`
                                             Linear   struct {
                                                     Duration       string `xml:"Duration"`
                                                     TrackingEvents struct {
                                                             Tracking []string `xml:"Tracking"`
                                                     } `xml:"TrackingEvents"`
                                                     VideoClicks struct {
                                                             ClickThrough string   `xml:"ClickThrough"`
                                                             CustomClick  []string `xml:"CustomClick"`
                                                     } `xml:"VideoClicks"`
                                                     MediaFiles struct {
                                                             MediaFile []struct {
                                                                     Delivery string `xml:"delivery,attr"`
                                                                     Bitrate  string `xml:"bitrate,attr"`
                                                                     Width    string `xml:"width,attr"`
                                                                     Height   string `xml:"height,attr"`
                                                                     Type     string `xml:"type,attr"`
                                                             } `xml:"MediaFile"`
                                                     } `xml:"MediaFiles"`
                                             } `xml:"Linear"`
                                             CompanionAds struct {
                                                     Companion []struct {
                                                             Width                 string `xml:"width,attr"`
                                                             Height                string `xml:"height,attr"`
                                                             StaticResource        string `xml:"StaticResource"`
                                                             TrackingEvents        string `xml:"TrackingEvents"`
                                                             CompanionClickThrough string `xml:"CompanionClickThrough"`
                                                     } `xml:"Companion"`
                                             } `xml:"CompanionAds"`
                                     } `xml:"Creative"`
                             } `xml:"Creatives"`
                             Extensions string `xml:"Extensions"`
                     } `xml:"InLine"`
             } `xml:"Ad"`
     } `xml:"VAST"`

}

   func main() 
{

    resp, err := http.Get("http://ad3.liverail.com/?LR_PUBLISHER_ID=1331&LR_CAMPAIGN_ID=229&LR_SCHEMA=vast2")

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    defer resp.Body.Close()

   xmlDataFromHttp, err := ioutil.ReadAll(resp.Body) 

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
   fmt.Println(os.Stdout, string(xmlDataFromHttp))

// read xml http response

   var xmlData VastHtml
   err = xml.Unmarshal(xmlDataFromHttp, &xmlData)
     if err != nil {
           panic(err)
         }

fmt.Printf("XML===>: ", xmlData.VAST)

 }

Hello everone, after Unmarshaling the XmlVast that I get from an URL that's give just the struct but without any value. even when i copy the content of the XmlVast in a file and I try to Unmarshal it it give me the same responce the struct without any value.

Thank you in advance.

  • 写回答

2条回答 默认 最新

  • dtttlua7165 2015-05-11 17:03
    关注

    You are trying to unmarshal an xml document with a root of <VAST> into a struct of type VastHtml, which contains VAST as its first field.

    Pass the VAST element directly to the Unmarshal function if that is what you want to decode into.

    err = xml.Unmarshal(xmlDataFromHttp, &xmlData.VAST)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动