使用Go的xml包进行DIDL-Lite的编组

huangapple go评论105阅读模式
英文:

Marshalling DIDL-Lite with Go's xml package

问题

这是一个来自UPnP AV ContentDirectory v2 Service Template的示例DIDL-Lite XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<DIDL-Lite
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"
 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>

如何使用Go的xml包将数据编组为这种格式?具体来说:

  1. 如何定义命名空间前缀,例如xmlns:dcxmlns:upnp
  2. 如何在元素上配置多个命名空间?
  3. 如何为属性设置命名空间,例如schemaLocation属性上的xsi前缀?

作为基础,我有类似这样的代码:

type DIDLLite struct {
    XMLName xml.Name `xml:"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ DIDL-Lite"`
    // ??? 命名空间前缀 dc, upnp, xsi
    Objects []Object
}

我还找到了可能相关的错误

英文:

Here's a sample DIDL-Lite XML document from the UPnP AV ContentDirectory v2 Service Template:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;DIDL-Lite
 xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot;
 xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot;
 xmlns:upnp=&quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot;
 xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
 xsi:schemaLocation=&quot;
   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">
   &lt;item id=&quot;18&quot; parentID=&quot;13&quot; restricted=&quot;0&quot;&gt;
      ...
   &lt;/item&gt;
&lt;/DIDL-Lite&gt;

How would one go about marshalling to this with Go's xml package? More specifically:

  1. How are namespace prefixes defined, such as xmlns:dc and xmlns:upnp?
  2. How are multiple name spaces configured on an element?
  3. How is the namespace set for attributes, such as the xsi prefix on the schemaLocation attribute?

As a base, I have something like this:

type DIDLLite struct {
    XMLName xml.Name `xml:&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ DIDL-Lite&quot;`
    // ??? namespace prefixes dc, upnp, xsi
    Objects []Object
}

I've also found this possibly related bug.

答案1

得分: 4

你给出的示例已经进行了编组。我假设你的意思是问:“如何定义能够与xml.Marshal一起编组的Go数据类型?”

package main

import (
    "encoding/xml"
    "fmt"
)

type DIDLLite struct {
    XMLName xml.Name
    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))
}

输出结果:

<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>

可以进行漂亮的打印以匹配你上面的示例。xml.MarshalIndent还有点原始。

英文:

The example you gave is marshalled. I assume you mean to ask, "how would one define Go data types that marshal with xml.Marshal into this?"

package main

import (
    &quot;encoding/xml&quot;
    &quot;fmt&quot;
)

type DIDLLite struct {
    XMLName xml.Name
    DC      string   `xml:&quot;xmlns:dc,attr&quot;`
    UPNP    string   `xml:&quot;xmlns:upnp,attr&quot;`
    XSI     string   `xml:&quot;xmlns:xsi,attr&quot;`
    XLOC    string   `xml:&quot;xsi:schemaLocation,attr&quot;`
    Objects []Object `xml:&quot;item&quot;`
}

type Object struct {
    ID         string `xml:&quot;id,attr&quot;`
    Parent     string `xml:&quot;parentID,attr&quot;`
    Restricted string `xml:&quot;restricted,attr&quot;`
}

func main() {
    d := DIDLLite{
        XMLName: xml.Name{
            Space: &quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot;,
            Local: &quot;DIDL-Lite&quot;,
        },
        DC:   &quot;http://purl.org/dc/elements/1.1/&quot;,
        UPNP: &quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot;,
        XSI:  &quot;http://www.w3.org/2001/XMLSchema-instance&quot;,
        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: &quot;18&quot;, Parent: &quot;13&quot;, Restricted: &quot;0&quot;}},
    }
    b, err := xml.MarshalIndent(d, &quot;&quot;, &quot;    &quot;)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(string(b))
}

Output:

&lt;DIDL-Lite xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot; xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:upnp=&quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;
   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">
    &lt;item id=&quot;18&quot; parentID=&quot;13&quot; restricted=&quot;0&quot;&gt;&lt;/item&gt;
&lt;/DIDL-Lite&gt;

which can be pretty printed to match your example above. xml.MarshallIndent is a little primitive yet.

huangapple
  • 本文由 发表于 2012年5月30日 16:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/10812294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定