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

huangapple go评论94阅读模式
英文:

XML Marshalling with Golang - Multiple Nodes with Identical Node Name

问题

VAST规范中,一个XML文件可以包含多个具有相同名称的节点,例如多个Impression节点,它们只在其id属性上有所不同。

考虑以下示例:

<?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&amp;ft_configuration=0&amp;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&amp;ft_width=640&amp;ft_height=360&amp;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>

请注意,多个Impression节点仅在其id属性上有所不同(实际上,有时甚至可能相同)。

有没有一种方法可以使用Golang表示这样的结构,并保持使用encoding/xml包进行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:

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

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?

答案1

得分: 6

对于重复的 XML 标签,在 Go 中可以简单地使用切片。

看下面这个简单的例子,它处理了你的 XML 输入,专注于解组 &lt;Impression&gt; 标签,然后再次编组它们:

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\n\n", v)

    if out, err := xml.MarshalIndent(v, "", "  "); err != nil {
        panic(err)
    } else {
        fmt.Println(string(out))
    }
}

输出结果(换行显示,可以在 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]: http://play.golang.org/p/JG0ontd6cO
英文:

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

See this simple example which handles your XML input focused on unmarshaling &lt;Impression&gt; tags, then marshaling them again:

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

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

&amp;{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:}]}
&lt;VAST&gt;
&lt;Ad&gt;
&lt;InLine&gt;
&lt;Impression id=&quot;ft_vast_i&quot;&gt;http://servedby.fla...[CUT]...1076585830&lt;/Impression&gt;
&lt;Impression id=&quot;3rdparty&quot;&gt;&lt;/Impression&gt;
&lt;Impression id=&quot;3rdparty&quot;&gt;&lt;/Impression&gt;
&lt;Impression id=&quot;3rdparty&quot;&gt;&lt;/Impression&gt;
&lt;Impression id=&quot;3rdparty&quot;&gt;&lt;/Impression&gt;
&lt;Impression id=&quot;3rdparty&quot;&gt;&lt;/Impression&gt;
&lt;Impression id=&quot;3rdparty&quot;&gt;&lt;/Impression&gt;
&lt;/InLine&gt;
&lt;/Ad&gt;
&lt;/VAST&gt;

答案2

得分: 1

是的,你可以使用切片来编组/解组具有相同名称的多个节点。

看一下encoding/xml示例中的Email切片:

type Email struct {
    Where string `xml:"where,attr"`
    Addr  string
}
type Address struct {
    City, State string
}
type Result struct {
    XMLName xml.Name `xml:"Person"`
    Name    string   `xml:"FullName"`
    Phone   string
    Email   []Email
    Groups  []string `xml:"Group>Value"`
    Address
}

对应的XML文档如下:

<Person>
  <FullName>Grace R. Emlin</FullName>
  <Company>Example Inc.</Company>
  <Email where="home">
    <Addr>gre@example.com</Addr>
  </Email>
  <Email where='work'>
    <Addr>gre@work.com</Addr>
  </Email>
  <Group>
    <Value>Friends</Value>
    <Value>Squash</Value>
  </Group>
  <City>Hanga Roa</City>
  <State>Easter Island</State>
</Person>

完整示例

英文:

Yes, you can marshal/unmarshal multiple nodes with the same names. Just use a slice.

Take a look at the Email slice in the encoding/xml example:

type Email struct {
Where string `xml:&quot;where,attr&quot;`
Addr  string
}
type Address struct {
City, State string
}
type Result struct {
XMLName xml.Name `xml:&quot;Person&quot;`
Name    string   `xml:&quot;FullName&quot;`
Phone   string
Email   []Email
Groups  []string `xml:&quot;Group&gt;Value&quot;`
Address
}

And the corresponding XML document:

&lt;Person&gt;
&lt;FullName&gt;Grace R. Emlin&lt;/FullName&gt;
&lt;Company&gt;Example Inc.&lt;/Company&gt;
&lt;Email where=&quot;home&quot;&gt;
&lt;Addr&gt;gre@example.com&lt;/Addr&gt;
&lt;/Email&gt;
&lt;Email where=&#39;work&#39;&gt;
&lt;Addr&gt;gre@work.com&lt;/Addr&gt;
&lt;/Email&gt;
&lt;Group&gt;
&lt;Value&gt;Friends&lt;/Value&gt;
&lt;Value&gt;Squash&lt;/Value&gt;
&lt;/Group&gt;
&lt;City&gt;Hanga Roa&lt;/City&gt;
&lt;State&gt;Easter Island&lt;/State&gt;
&lt;/Person&gt;

(full example)

huangapple
  • 本文由 发表于 2015年12月17日 19:56:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/34334008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定