英文:
How to deserialize same XML Element with different namespace into different elements in a struct
问题
我正在使用以下类型的XML结构进行工作:
<?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>
我在尝试将<description>
和<media:description>
反序列化为结构体中的两个不同元素时遇到了问题。
我尝试了以下类型的结构体来表示<item>
,但是media:description
的值最终出现在结构体中。
type Item struct {
// ...其他字段
Description string `xml:"description"`
MediaDescription string `xml:"media:description"`
// ...其他字段
}
有什么最好的方法可以解决这个问题?
英文:
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
得分: 0
所以目前Go无法支持这一点(正如@JimB所指出的),但您仍然可以使用一些内置功能来提取正确的元素。首先,定义一个新的结构体XMLElement
:
type XMLElement struct {
XMLName xml.Name
Data string `xml:",chardata"`
}
这个结构体可以捕获XML数据以及命名空间和元素名称。接下来,将xml:"description"
映射到XMLElement
数组。这将捕获所有<*:description>
元素并存储在列表中。然后,您可以定义函数来提取正确的元素。
type Item struct {
// ...其他字段
Descriptions []XMLElement `xml:"description"`
// ...其他字段
}
// GetDescription返回<description>标签中的描述
func (i *Item) GetDescription() string {
for _, elem := range i.Descriptions {
if elem.XMLName.Space == "" {
return elem.Data
}
}
return ""
}
// GetMediaDescription返回<media:description>标签中的描述
func (i *Item) GetMediaDescription() string {
for _, elem := range i.Descriptions {
if elem.XMLName.Space == "http://search.yahoo.com/mrss/" {
return elem.Data
}
}
return ""
}
以上是代码的翻译部分。
英文:
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 ""
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论