duanjizi9443 2015-12-17 11:56
浏览 283
已采纳

使用Golang进行XML编组-具有相同节点名称的多个节点

In VAST spec, an XML file may contains several nodes with the same name - for example, several Impression nodes, which differ only in their id.

Consider the following example:

<?xml version="1.0" encoding="UTF-8"?>
<VAST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:noNamespaceSchemaLocation="vast.xsd">
   <Ad id="812030">
      <InLine>
         <AdSystem>FT</AdSystem>
         <AdTitle>Flashtalking mobile vast template 2.0</AdTitle>
         <Description>date of revision 10-04-14</Description>
         <Impression id="ft_vast_i"><![CDATA[http://servedby.flashtalking.com/imp/1/31714;812030;201;gif;DailyMail;640x360VASTHTML5/?ft_creative=377314&ft_configuration=0&cachebuster=1076585830]]></Impression>
         <Impression id="3rdparty" />
         <Impression id="3rdparty" />
         <Impression id="3rdparty" />
         <Impression id="3rdparty" />
         <Impression id="3rdparty" />
         <Impression id="3rdparty" />
         <Creatives>
            <Creative sequence="1">
               <Linear>
                  <Duration>00:00:15</Duration>
                  <TrackingEvents>
                     <Tracking event="start"><![CDATA[http://stat.flashtalking.com/reportV3/ft.stat?34923237-0-13-0-27565A88066DCD-1076585830]]></Tracking>
                     <Tracking event="midpoint"><![CDATA[http://stat.flashtalking.com/reportV3/ft.stat?34923237-0-15-0-27565A88066DCD-1076585830]]></Tracking>
                     <Tracking event="firstQuartile"><![CDATA[http://stat.flashtalking.com/reportV3/ft.stat?34923237-0-14-0-27565A88066DCD-1076585830]]></Tracking>
                     <Tracking event="thirdQuartile"><![CDATA[http://stat.flashtalking.com/reportV3/ft.stat?34923237-0-16-0-27565A88066DCD-1076585830]]></Tracking>
                     <Tracking event="complete"><![CDATA[http://stat.flashtalking.com/reportV3/ft.stat?34923237-0-17-0-27565A88066DCD-1076585830]]></Tracking>
                     <Tracking event="mute"><![CDATA[http://stat.flashtalking.com/reportV3/ft.stat?34923237-0-38-0-27565A88066DCD-1076585830]]></Tracking>
                     <Tracking event="fullscreen"><![CDATA[http://stat.flashtalking.com/reportV3/ft.stat?34923237-0-313-0-27565A88066DCD-1076585830]]></Tracking>
                  </TrackingEvents>
                  <VideoClicks>
                     <ClickThrough><![CDATA[http://servedby.flashtalking.com/click/1/31714;812030;377314;211;0/?random=1076585830&ft_width=640&ft_height=360&url=http://www.google.co.uk]]></ClickThrough>
                  </VideoClicks>
                  <MediaFiles>
                     <MediaFile id="1" delivery="progressive" type="video/mp4" bitrate="524" width="640" height="360"><![CDATA[http://cdn.flashtalking.com/17601/30988_26752_WacoClub_640x360_384kbps.mp4]]></MediaFile>
                  </MediaFiles>
               </Linear>
            </Creative>
            <Creative sequence="1">
               <CompanionAds />
            </Creative>
         </Creatives>
      </InLine>
   </Ad>
</VAST>

Note the multiple Impression nodes which differ only by their id attribute (and in fact even that may sometime be identical).

Is there a way to represent such structure with Golang in a way that will maintain the ability to marshal and unmarshal the xml with the encoding/xml package?

  • 写回答

2条回答 默认 最新

  • doukou1718 2015-12-17 12:10
    关注

    For repeating XML tags, simply use a slice in Go.

    See this simple example which handles your XML input focused on unmarshaling <Impression> tags, then marshaling them again:

    type Impression struct {
        Id      string `xml:"id,attr"`
        Content string `xml:",chardata"`
    }
    
    type VAST struct {
        Impressions []Impression `xml:"Ad>InLine>Impression"`
    }
    
    func main() {
        v := &VAST{}
        if err := xml.Unmarshal([]byte(src), v); err != nil {
            panic(err)
        }
        fmt.Printf("%+v
    
    ", v)
    
        if out, err := xml.MarshalIndent(v, "", "  "); err != nil {
            panic(err)
        } else {
            fmt.Println(string(out))
        }
    }
    

    Output (wrapped, try it on the Go Playground):

    &{Impressions:[{Id:ft_vast_i Content:http://servedby.fla...[CUT]...1076585830} 
    {Id:3rdparty Content:} {Id:3rdparty Content:} {Id:3rdparty Content:} 
    {Id:3rdparty Content:} {Id:3rdparty Content:} {Id:3rdparty Content:}]}
    
    <VAST>
      <Ad>
        <InLine>
          <Impression id="ft_vast_i">http://servedby.fla...[CUT]...1076585830</Impression>
          <Impression id="3rdparty"></Impression>
          <Impression id="3rdparty"></Impression>
          <Impression id="3rdparty"></Impression>
          <Impression id="3rdparty"></Impression>
          <Impression id="3rdparty"></Impression>
          <Impression id="3rdparty"></Impression>
        </InLine>
      </Ad>
    </VAST>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题