登录
首页 >  Golang >  Go问答

在 Golang 中解组一个简单的 xml 时出错

来源:Golang技术栈

时间:2023-04-30 09:16:46 491浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《在 Golang 中解组一个简单的 xml 时出错》,就很适合你,本篇文章讲解的知识点主要包括golang。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我正在尝试在 Go 中为大型 xml 文件([dblp.xml](https://dblp.uni- trier.de/))编写一个非常简单的解析器,其摘录如下:

Craig GentryComputing arbitrary functions of encrypted data.97-105201053Commun. ACM3http://doi.acm.org/10.1145/1666420.1666444db/journals/cacm/cacm53.html#Gentry10
Craig Gentry Number2Computing arbitrary functions of encrypted data.97-105201053Commun. ACM3http://doi.acm.org/10.1145/1666420.1666444db/journals/cacm/cacm53.html#Gentry10

我的代码如下,看起来好像发生了什么事xml.Unmarshal(byteValue, &articles),因为我无法在输出中获取任何 xml 值。你能帮我解决我的代码有什么问题吗?

package main

import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "os"
)

// Contains the array of articles in the dblp xml
type Dblp struct {
    XMLName xml.Name  `xml:"dblp"`
    Dblp    []Article `xml:"article"`
}

// Contains the article element tags and attributes
type Article struct {
    XMLName xml.Name `xml:"article"`
    Key     string   `xml:"key,attr"`
    Year    string   `xml:"year"`
}

func main() {
    xmlFile, err := os.Open("TestDblp.xml")
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println("Successfully Opened TestDblp.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 articles Dblp
    fmt.Println("Entered var")
    // we unmarshal our byteArray which contains our
    // xmlFiles content into 'users' which we defined above
    xml.Unmarshal(byteValue, &articles)

    for i := 0; i 

正确答案

您的代码中有特定行返回错误

xml.Unmarshal(byteValue, &articles)

如果您将其更改为

err = xml.Unmarshal(byteValue, &articles)
if err != nil {
    fmt.Println(err.Error())
}

你会看到一个错误被报告:xml: encoding "ISO-8859-1" declared but Decoder.CharsetReader is nil。作为最佳实践,您应该始终检查返回的错误。

要解决此问题,您可以encoding="ISO-8859-1"从 XML 中删除编码属性 ( ) 或稍微更改解组代码:

package main

import (
    "encoding/xml"
    "fmt"
    "io"
    "os"

    "golang.org/x/text/encoding/charmap"
)

// Contains the array of articles in the dblp xml
type Dblp struct {
    XMLName xml.Name  `xml:"dblp"`
    Dblp    []Article `xml:"article"`
}

// Contains the article element tags and attributes
type Article struct {
    XMLName xml.Name `xml:"article"`
    Key     string   `xml:"key,attr"`
    Year    string   `xml:"year"`
}

func main() {
    xmlFile, err := os.Open("dblp.xml")
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println("Successfully Opened TestDblp.xml")
    // defer the closing of our xmlFile so that we can parse it later on
    defer xmlFile.Close()

    var articles Dblp
    decoder := xml.NewDecoder(xmlFile)
    decoder.CharsetReader = makeCharsetReader
    err = decoder.Decode(&articles)
    if err != nil {
        fmt.Println(err)
    }

    for i := 0; i 

运行上述程序会导致:

Successfully Opened TestDblp.xml
Entered var
Entered loop
get title: journals/cacm/Gentry10
get year: 2010
Entered loop
get title: journals/cacm/Gentry10
get year: 2010

以上就是《在 Golang 中解组一个简单的 xml 时出错》的详细内容,更多关于golang的资料请关注golang学习网公众号!

声明:本文转载于:Golang技术栈 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>