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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示