如何使用 golang 解组 rdf xml?
我可以 Unmarshal 简单的 xml,但是当我尝试 Unmarshal rdf xml 时,我什么也没得到。
目前我需要解析下面的 xml:
<?xml version='1.0' encoding='UTF-8'?>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
<rdf:Description rdf:about='about'
xmlns:et='http://ns.exiftool.org/1.0/' et:toolkit='Image::ExifTool 12.30'
xmlns:ExifTool='http://ns.exiftool.org/ExifTool/1.0/'
xmlns:System='http://ns.exiftool.org/File/System/1.0/'
xmlns:File='http://ns.exiftool.org/File/1.0/'>
<ExifTool:ExifToolVersion>12.30</ExifTool:ExifToolVersion>
<System:FileSize>20 MiB</System:FileSize>
</rdf:Description>
</rdf:RDF>
下面是我用来解析 xml 的 go
代码:
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
type MyXml struct {
XMLName xml.Name `xml:"rdf:RDF"`
Rdf string `xml:"rdf,attr"`
}
func main() {
// Open our xmlFile
xmlFile, err := os.Open("./z.xml")
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println("err", err)
}
fmt.Println("Successfully Opened z.xml")
// defer the closing of our xmlFile so that we can parse it later on
defer xmlFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(xmlFile)
var data MyXml
xml.Unmarshal(byteValue, &data)
fmt.Println(data)
}
无论我设置什么 XMLName
我总是得到 {{ } }
。
我需要 System:FileSize
但没有得到任何数据。
I can can Unmarshal simple xmls but when i try to Unmarshal rdf xml, i dont't get anything.
Currently i need to parse the below xml:
<?xml version='1.0' encoding='UTF-8'?>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
<rdf:Description rdf:about='about'
xmlns:et='http://ns.exiftool.org/1.0/' et:toolkit='Image::ExifTool 12.30'
xmlns:ExifTool='http://ns.exiftool.org/ExifTool/1.0/'
xmlns:System='http://ns.exiftool.org/File/System/1.0/'
xmlns:File='http://ns.exiftool.org/File/1.0/'>
<ExifTool:ExifToolVersion>12.30</ExifTool:ExifToolVersion>
<System:FileSize>20 MiB</System:FileSize>
</rdf:Description>
</rdf:RDF>
below is the go
code that i am using to parse the xml:
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
type MyXml struct {
XMLName xml.Name `xml:"rdf:RDF"`
Rdf string `xml:"rdf,attr"`
}
func main() {
// Open our xmlFile
xmlFile, err := os.Open("./z.xml")
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println("err", err)
}
fmt.Println("Successfully Opened z.xml")
// defer the closing of our xmlFile so that we can parse it later on
defer xmlFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(xmlFile)
var data MyXml
xml.Unmarshal(byteValue, &data)
fmt.Println(data)
}
And no matter what i set XMLName
i always get {{ } }
.
I need System:FileSize
but getting nothing in data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论