用Go的xml软件包编组DIDL-Lite [英] Marshalling DIDL-Lite with Go's xml package
问题描述
以下是一个 DIDL-Lite XML文档示例 UPnP AV ContentDirectory v2服务模板:
<?xml version =1.0encoding =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。组织/模式/ AV / UPnP的v2-20061231.xsd>
< item id =18parentID =13restricted =0>
...
< / item>
< / DIDL-Lite>
如何使用编组 http://golang.org/pkg/encoding/xml/rel =nofollow> Go的xml包?更具体地说:
- 定义名称空间前缀的方式,比如
xmlns:dc
和xmlns:upnp
? - 如何在元素上配置多个名称空间?是为属性设置的名称空间,例如
schemaLocation
属性中的xsi
前缀 - How are namespace prefixes defined, such as
xmlns:dc
andxmlns:upnp
? - How are multiple name spaces configured on an element?
- How is the namespace set for attributes, such as the
xsi
prefix on theschemaLocation
attribute?
作为一个基地,我有这样的事情:
type DIDLLite struct {
XMLName xml.Name`xml:urn:schemas-upnp-org:metadata-1-0 / DIDL-Lite / DIDL-Lite`
// ???命名空间前缀dc,upnp,xsi
Objects [] Object
}
I还发现了此可能存在的相关错误。
您给的例子是
package
$ b $ .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字符串`xml:id,attr`
父字符串`xml:parentID,attr`
受限字符串`xml:restricted,attr`
func main(){
d:= DIDLLite {
XMLName:xml.Name {
空格:urn:schemas-upnp-org:metadata- 1-0 / DIDL-Lite /,
本地: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: schema-upnp-org:metadata-1-0 / DIDL-Lite /
http://www.upnp.org/schemas/av/didl-lite-v2-20060531.xsd
urn:模式-upnp-org:metadata-1-0 / upnp /
http:// www.upnp.org/ schemas / av / upnp-v2-20061231.xsd`,
对象:[] Object { {ID:18,Parent:13,Restricted:0}},
}
b,err:= xml.MarshalIndent(d,,)
如果err!= nil {
fmt.Println(err)
return
}
fmt.Println(string(b))
}
<
$ 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-instancexsi:schemaLocation =
urn: schemas-upnp-org:metadata-1-0 / DIDL-Lite /
http://www.upnp.org/schema s / 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 =18parentID =13restricted =0>< / item>
< / DIDL-Lite>
可以很漂亮地打印以符合上述示例。 xml.MarshallIndent还有点原始。
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:
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.
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.
这篇关于用Go的xml软件包编组DIDL-Lite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!