如何在固定结构中解组动态 XML?

问题描述

在固定结构中解组不同的 XML 时遇到问题。 这是一些 XML 示例:

<?xml version="1.0" encoding="utf-8"?>
<RSS>
<channel>
 <title>Città di Firenze - Sito istituzionale del comune di Firenze</title>
 <link>https://www.comune.fi.it/</link>
 <description/>
 <language>it</language>
  <item>
   <title>Convocazione Consiglio di Quartiere 3</title>
    <link>https://www.comune.fi.it/dalle-redazioni/convocazione-consiglio-di-quartiere-3-8</link>
    <description> &lt;br /&gt; Venerdì 29 Gennaio alle ore 18,00 è convocato il Consiglio di Quartiere in seduta telematica e potrà essere seguita in streaming;</description>
    <pubDate>Thu,28 Jan 2021 12:13:50 +0100</pubDate>
    <dc:creator>Redazione</dc:creator>
    <guid isPermaLink="false">1761cfa1-afc4-4ded-b394-fe789229082a</guid>
  </item>
</channel>
</RSS>
<response>
 <item key="0">
   <field_bulletin_extra_date>24 ott 2020</field_bulletin_extra_date>
   <field_bulletin_extra_date_1>Sat,24 Oct 2020 12:00:00+0000</field_bulletin_extra_date_1>
   <title>24 ott 2020</title>
   <field_abstract>Grazie </field_abstract>
   <uid>Redazione</uid>
   <view_node>Link</view_node>
   <field_parole_chiave>mobilità,IF,sicurezza sTradale</field_parole_chiave>
   <field_contenuto_articolo>Dopo </field_contenuto_articolo>
</item>
</response>

我创建了不同的结构:

type RedazioneComuneFiData struct {
    XMLName  xml.Name `xml:"RSS"`
    Title    *string  `xml:"channel>title"`
    Link     *string  `xml:"channel>link"`
    Language *string  `xml:"channel>language"`
    ItemList []*Item  `xml:"channel>item"`
}

type Item struct {
    Title       *string `xml:"title"`
    Link        *string `xml:"link"`
    Description *string `xml:"description"`
    PubDate     *string `xml:"pubDate"`
    Creator     *string `xml:"dc:creator"`
}
type ComunicatiStampaComuneFiData struct {
    XMLName  xml.Name                        `xml:"response"`
    ItemList []*ComunicatiStampaComuneFiItem `xml:"item"`
}

type ComunicatiStampaComuneFiItem struct {
    Title         *string `xml:"title"`
    ID            *string `xml:"uid"`
    FieldAbstract *string `xml:"field_abstract"` // description or abstract
    ViewNode      *string `xml:"view_node"`      // link
    ReadableDate  *string `xml:"field_bulletin_extra_date"`
    Date          *string `xml:"field_bulletin_extra_date_1"`
    Content       *string `xml:"field_contenuto_articolo"`
    Keywords      *string `xml:"field_parole_chiave"`
}

对于解组,我只是做了:

var comunicatiStampaComuneFiData ComunicatiStampaComuneFiData
err = xml.Unmarshal(contents,&comunicatiStampaComuneFiData)
var redazioneComuneFiResponse RedazioneComuneFiData
err = xml.Unmarshal(contents,&redazioneComuneFiResponse)

我需要区分结构,因为我必须在其他部分使用它们的字段,但我也想概括一些未来 XML 的解组过程。我尝试实现 xml.Unmarshaller 接口,但没有成功。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)