dpk20361 2017-01-04 22:57
浏览 367
已采纳

如何将具有不同名称空间的相同XML元素反序列化为结构中的不同元素

I am working with the following kind of an XML structure

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" 
    xmlns:content="http://purl.org/rss/1.0/modules/content/" 
    xmlns:media="http://search.yahoo.com/mrss/">
    <channel>
        <title>TITLE</title>
        <link>http://something.com</link>
        <description>description</description>
        <lastBuildDate>Mon, 19 Dec 2016 16:48:54 +0000</lastBuildDate>
        <language>en</language>
        <item>
            <title>Title</title>
            <description>description</description>
            <author>
                <name>Name</name>
                <uri></uri>
            </author>
            <pubDate>Mon, 19 Dec 2016 16:42:32 +0000</pubDate>
            <link>http://google.com</link>
            <image>...</image>
            <media:description><![CDATA[Natalie Massenet]]></media:description>
            <media:credit>David Fisher/REX/Shutterstock</media:credit>
            <category>Category1</category>
            <category>Category2</category>
            <guid isPermaLink="false">http://something.com/?p=10730393</guid>
            <media:group></media:group>
            <content:encoded>content</content:encoded>
        </item>
    </channel>
</rss>

I am having trouble figuring out how to deserialize <description> and <media:description> into two different element in a struct.

I've tried a following kind of a struct to represent an <item>, but the value of media:description ends up in the struct.

type Item struct {
    // ...other fields
    Description           string   `xml:"description"`
    MediaDescription      string   `xml:"media:description"`
    // ...other fields
}

What would be the best way to do this?

  • 写回答

1条回答 默认 最新

  • duanjue2560 2017-01-06 22:28
    关注

    So Go currently is not able to support this (as pointed out by @JimB), but you can still use some built in capabilities to pull out the right element. Start off by defining a new struct XMLElement

    type XMLElement struct {
        XMLName xml.Name
        Data    string `xml:",chardata"`
    }
    

    This struct can capture the xml data along with the namespace and element name. Next map xml:"description" to an array of XMLElement. This will end up capturing all <*:description> elements in a list. You can then define functions to pull out the right one.

    type Item struct {
        // ...other fields
        Descriptions           []XMLElement   `xml:"description"`
        // ...other fields
    }
    
    // GetDescription returns the description in the <description> tag
    func (i *Item) GetDescription() string {
        for _, elem := range i.Descriptions {
            if elem.XMLName.Space == "" {
                return elem.Data
            }
        }
        return ""
    }
    
    // GetMediaDescription returns the description in the <media:description> tag
    func (i *Item) GetMediaDescription() string {
        for _, elem := range i.Descriptions {
            if elem.XMLName.Space == "http://search.yahoo.com/mrss/" {
                return elem.Data
            }
        }
        return ""
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器