如何使用 golang 解组 rdf xml?

发布于 2025-01-09 19:48:26 字数 1550 浏览 2 评论 0原文

我可以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文