duan97689 2012-05-30 08:02
浏览 66
已采纳

用Go的xml包编组DIDL-Lite

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

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

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:"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ DIDL-Lite"`
    // ??? namespace prefixes dc, upnp, xsi
    Objects []Object
}

I've also found this possibly related bug.

  • 写回答

1条回答 默认 最新

  • douyi1939 2012-05-30 18:06
    关注

    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 (
        "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))
    }
    

    Output:

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站