boxmoe_header_banner_img

Hello! 欢迎来到悠悠畅享网!

文章导读

使用 Go 的 xml 包编组 DIDL-Lite 数据


avatar
作者 2025年9月4日 10

使用 Go 的 xml 包编组 DIDL-Lite 数据

本文档介绍了如何使用 go 语言的 encoding/xml 包将数据编组为 DIDL-Lite XML 格式。重点讲解了如何定义 XML 命名空间前缀、配置元素的多个命名空间以及为属性设置命名空间。通过示例代码,展示了如何将 Go 结构体转换为符合 UPnP AV ContentDirectory v2 服务模板的 DIDL-Lite XML 文档。

使用 encoding/xml 包编组 DIDL-Lite XML 数据

Go 语言的 encoding/xml 包提供了编组(marshalling)和解组(unmarshalling) XML 数据的能力。要将 Go 结构体编组为特定的 XML 格式,需要使用 XML 标签来定义结构体字段与 XML 元素和属性之间的映射关系。

以下代码展示了如何使用 encoding/xml 包将 Go 结构体编组为 DIDL-Lite XML 格式:

package main  import (     "encoding/xml"     "fmt" )  type DIDLLite struct {     XMLName xml.Name `xml:"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ DIDL-Lite"`     DC      string   `xml:"xmlns:dc,attr"`     UPNP    string   `xml:"xmlns:upnp,attr"`     XSI     string   `xml:"xmlns:xsi,attr"`     XLOC    string   `xml:"xsi:schemaLocation,attr"`     Objects []Object `xml:"item"` }  type Object struct {     ID         string `xml:"id,attr"`     Parent     string `xml:"parentID,attr"`     Restricted string `xml:"restricted,attr"` }  func main() {     d := DIDLLite{         XMLName: xml.Name{             Space: "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/",             Local: "DIDL-Lite",         },         DC:   "http://purl.org/dc/elements/1.1/",         UPNP: "urn:schemas-upnp-org:metadata-1-0/upnp/",         XSI:  "http://www.w3.org/2001/XMLSchema-instance",         XLOC: `    urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/     http://www.upnp.org/schemas/av/didl-lite-v2-20060531.xsd    urn:schemas-upnp-org:metadata-1-0/upnp/     http://www.upnp.org/schemas/av/upnp-v2-20061231.xsd`,         Objects: []Object{{ID: "18", Parent: "13", Restricted: "0"}},     }     b, err := xml.MarshalIndent(d, "", "    ")     if err != nil {         fmt.Println(err)         return     }     fmt.Println(string(b)) }

代码解释:

使用 Go 的 xml 包编组 DIDL-Lite 数据

悦灵犀AI

一个集AI绘画、问答、创作于一体的一站式AI工具平台

使用 Go 的 xml 包编组 DIDL-Lite 数据67

查看详情 使用 Go 的 xml 包编组 DIDL-Lite 数据

  • DIDLLite 结构体: 定义了 DIDL-Lite XML 文档的结构。
    • XMLName 字段使用 xml:”urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ DIDL-Lite” 标签指定了根元素的命名空间和本地名称。
    • DC, UPNP, XSI 字段使用 xml:”xmlns:dc,attr”, xml:”xmlns:upnp,attr”, xml:”xmlns:xsi,attr” 标签定义了命名空间前缀,并将其作为属性添加到根元素。attr 关键字表示该字段对应 XML 属性。
    • XLOC 字段使用 xml:”xsi:schemaLocation,attr” 标签定义了 schemaLocation 属性,并使用 xsi 前缀指定了命名空间。
    • Objects 字段使用 xml:”item” 标签指定了 item 元素的集合。
  • Object 结构体: 定义了 item 元素的结构。
    • ID, Parent, Restricted 字段使用 xml:”id,attr”, xml:”parentID,attr”, xml:”restricted,attr” 标签定义了属性。
  • main 函数:
    • 创建了一个 DIDLLite 结构体实例,并填充了数据。
    • 使用 xml.MarshalIndent 函数将结构体编组为 XML 数据,并使用 ” ” 进行缩进,使其更易读。
    • 打印生成的 XML 数据。

运行结果:

<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="    urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/     http://www.upnp.org/schemas/av/didl-lite-v2-20060531.xsd    urn:schemas-upnp-org:metadata-1-0/upnp/     http://www.upnp.org/schemas/av/upnp-v2-20061231.xsd">     <item id="18" parentID="13" restricted="0"></item> </DIDL-Lite>

关键点总结

  • 命名空间定义: 使用 xmlns:prefix 属性在根元素上定义命名空间。在 Go 结构体中,使用 xml:”xmlns:prefix,attr” 标签来表示。
  • 属性命名空间: 使用 prefix:Attribute 的形式来指定属性的命名空间。在 Go 结构体中,使用 xml:”prefix:attribute,attr” 标签来表示。
  • 根元素命名空间: 通过设置 XMLName 字段的 Space 属性来指定根元素的命名空间。
  • xml.MarshalIndent 函数: 用于生成格式化的 XML 数据,使其更易读。

注意事项

  • 确保结构体字段的类型与 XML 元素的类型匹配。
  • 仔细检查 XML 标签的语法,确保其正确无误。
  • xml.MarshalIndent 的格式化功能比较基础,如果需要更复杂的格式化,可能需要自定义实现。

通过理解和应用这些概念,你可以使用 Go 语言的 encoding/xml 包灵活地编组各种 XML 数据,包括复杂的 DIDL-Lite 格式。



评论(已关闭)

评论已关闭