首頁  >  文章  >  後端開發  >  如何讓golang區分有命名空間和沒有命名空間的XML元素?

如何讓golang區分有命名空間和沒有命名空間的XML元素?

WBOY
WBOY轉載
2024-02-09 12:00:091142瀏覽

如何讓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: %s\n", err)
    return
}
for _, img := range resp.Image{
    if img.URL != "" {
        //<image>, not <itunes:image>
    }
}

處理解碼

我遇到的問題是,看起來像image []struct 認為<image></image> 都是<image></image> 元素,因為它們都有「圖片」。為了過濾掉<image>,我使用的方法是讓<code>image []struct 中的每個人檢查其url 是否為空字串(因為對於<image> 其<code>url 是屬性)。

另一種方法是寫我自己的 unmarshal 函數來區分具有和不具有 itunes 命名空間的 xml 元素。基本上我希望 image []struct 只保存元素 <image></image>

我想知道go是否有一些內建的功能來區分?還是我必須寫程式碼來過濾掉 <image></image>

解決方法

請注意,欄位順序很重要。 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: %s\n", err)
        return
    }
    fmt.printf("itunesimage: %v\n", resp.itunesimage)
    fmt.printf("images: %v\n", resp.image)
}

如果您只需要 <image></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: %s\n", err)
        return
    }
    for _, img := range resp.image {
        fmt.printf("namespace: %q, url: %s\n", img.xmlname.space, img.url)
    }
}

上述示範的輸出是:

namespace: "", url: http://sampleUrl.com
namespace: "http://www.itunes.com/dtds/podcast-1.0.dtd", url: http://sampleItunesUrl.com/

以上是如何讓golang區分有命名空間和沒有命名空間的XML元素?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除