亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Golang 進行 XML 編組 - 具有相同節點名稱的多個節點

使用 Golang 進行 XML 編組 - 具有相同節點名稱的多個節點

Go
冉冉說 2021-11-29 16:37:26
在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&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>                  </TrackingEvents>                  <VideoClicks>                  </VideoClicks>                  <MediaFiles>                  </MediaFiles>               </Linear>            </Creative>            <Creative sequence="1">               <CompanionAds />            </Creative>         </Creatives>      </InLine>   </Ad></VAST>注意多個Impression節點,它們的不同之處僅在于它們的id屬性(事實上,有時甚至可能是相同的)。有沒有辦法用 Golang 來表示這種結構,以保持使用encoding/xml包對 xml 進行編組和解組的能力?
查看完整描述

2 回答

?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

對于重復的 XML 標簽,只需在 Go 中使用切片即可。


請參閱這個簡單的示例,它處理您的 XML 輸入,重點是解組<Impression>標簽,然后再次對其進行編組:


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>


查看完整回答
反對 回復 2021-11-29
?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

是的,您可以編組/解組多個具有相同名稱的節點。只需使用切片。


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


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>[email protected]</Addr>

  </Email>

  <Email where='work'>

    <Addr>[email protected]</Addr>

  </Email>

  <Group>

    <Value>Friends</Value>

    <Value>Squash</Value>

  </Group>

  <City>Hanga Roa</City>

  <State>Easter Island</State>

</Person>


查看完整回答
反對 回復 2021-11-29
  • 2 回答
  • 0 關注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號