卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34765本站已运行393

如何让golang区分带命名空间和不带命名空间的XML元素?

如何让golang区分带命名空间和不带命名空间的xml元素?

php小编子墨将为大家分享如何让golang区分带命名空间和不带命名空间的XML元素的方法。在处理XML数据时,命名空间是一个重要的概念,它可以帮助我们更好地组织和区分不同的XML元素。本文将介绍如何使用golang的xml包来解析和处理带命名空间和不带命名空间的XML元素,并提供一些实际应用的示例代码。无论您是初学者还是有一定经验的开发者,都能从本文中获得有价值的知识和技巧。让我们一起来探索这个有趣而实用的话题吧!

问题内容

假设我有以下 xml 数据:

<image> 
  <url> http://sampleurl.com
  </url>
</image>

<itunes:image url="http://sampleitunesurl.com" />  //xmldata

我使用这个结构来解码它:

type response struct {
   xmlname   xml.name   `xml:"resp"`
   image []struct {
        url string   `xml:"url"`
  } `xml:"image"`
  itunesimage  struct {
       url string `xml:"url,attr"`
  }  `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd image"`

}

我有这样的代码:

var resp Response 
err := xml.Unmarshal([]byte(xmlData), &resp)
if err != nil {
    fmt.Printf("Error decoding XML: %sn", err)
    return
}
for _, img := range resp.Image{
    if img.URL != "" {
        //<image>, not <itunes:image>
    }
}

处理解码

我遇到的问题是,看起来像 image []struct 认为 都是 元素,因为它们都有“图像”。为了过滤掉 ,我使用的方法是让 image []struct 中的每个人检查其 url 是否为空字符串(因为对于 url 是属性)。

另一种方法是编写我自己的 unmarshal 函数来区分具有和不具有 itunes 命名空间的 xml 元素。基本上我希望 image []struct 仅保存元素

我想知道go是否有一些内置的功能来区分?或者我必须编写代码来过滤掉

解决方法

请注意,字段顺序很重要。 itunesimage 有一个更具体的标签,因此它应该位于 image 之前。

package main

import (
    "encoding/xml"
    "fmt"
)

func main() {
    type response struct {
        xmlname     xml.name `xml:"resp"`
        itunesimage struct {
            url string `xml:"url,attr"`
        } `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd image"`
        image []struct {
            url string `xml:"url"`
        } `xml:"image"`
    }

    xmldata := `
<resp xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  <image>
    <url>http://sampleurl.com</url>
  </image>
  <itunes:image url="http://sampleitunesurl.com/" />
</resp>
`

    var resp response
    err := xml.unmarshal([]byte(xmldata), &resp)
    if err != nil {
        fmt.printf("error decoding xml: %sn", err)
        return
    }
    fmt.printf("itunesimage: %vn", resp.itunesimage)
    fmt.printf("images: %vn", resp.image)
}

如果您只需要 标签中的 image/url ,您可以像这样定义 response 结构:

type response struct {
    xmlname xml.name `xml:"resp"`
    image   []string `xml:"image>url"`
}

关于标题中的一般问题(如何让golang区分带命名空间和不带命名空间的xml元素?),您可以使用名为xmlname的字段捕获元素名称,并检查该字段的space成员。请参阅下面的演示:

package main

import (
    "encoding/xml"
    "fmt"
)

func main() {
    type response struct {
        xmlname xml.name `xml:"resp"`
        image   []struct {
            xmlname xml.name
            url     string `xml:"url"`
        } `xml:"image"`
    }

    xmldata := `
<resp xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  <image>
    <url>http://sampleurl.com</url>
  </image>
  <itunes:image>
    <url>http://sampleitunesurl.com/</url>
  </itunes:image>
</resp>
`

    var resp response
    err := xml.unmarshal([]byte(xmldata), &resp)
    if err != nil {
        fmt.printf("error decoding xml: %sn", err)
        return
    }
    for _, img := range resp.image {
        fmt.printf("namespace: %q, url: %sn", img.xmlname.space, img.url)
    }
}

上述演示的输出是:

namespace: "", url: http://sampleUrl.com
namespace: "http://www.itunes.com/dtds/podcast-1.0.dtd", url: http://sampleItunesUrl.com/
卓越飞翔博客
上一篇: 无法在 K8S pod 上执行 shell
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码: