duanhu7390 2019-02-27 22:11
浏览 193
已采纳

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

I'm trying to write a very simple parser in Go for a large xml file (the dblp.xml), an excerpt of which is below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE dblp SYSTEM "dblp.dtd">
<dblp>
    <article key="journals/cacm/Gentry10" mdate="2010-04-26">
        <author>Craig Gentry</author>
        <title>Computing arbitrary functions of encrypted data.</title>
        <pages>97-105</pages>
        <year>2010</year>
        <volume>53</volume>
        <journal>Commun. ACM</journal>
        <number>3</number>
        <ee>http://doi.acm.org/10.1145/1666420.1666444</ee>
        <url>db/journals/cacm/cacm53.html#Gentry10</url>
    </article>

    <article key="journals/cacm/Gentry10" mdate="2010-04-26">
        <author>Craig Gentry Number2</author>
        <title>Computing arbitrary functions of encrypted data.</title>
        <pages>97-105</pages>
        <year>2010</year>
        <volume>53</volume>
        <journal>Commun. ACM</journal>
        <number>3</number>
        <ee>http://doi.acm.org/10.1145/1666420.1666444</ee>
        <url>db/journals/cacm/cacm53.html#Gentry10</url>
    </article>
</dblp>

My code is as follows and it looks like there's something going on at xml.Unmarshal(byteValue, &articles), as I cannot get any of the xml's values in the output. Can you help me with what is wrong with my code?

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 < len(articles.Dblp); i++ {
        fmt.Println("Entered loop")
        fmt.Println("get title: " + articles.Dblp[i].Key)
        fmt.Println("get year: " + articles.Dblp[i].Year)
    }

}
  • 写回答

1条回答 默认 最新

  • duanrong5167 2019-02-27 22:47
    关注

    You have a specific line in your code that returns an error

    xml.Unmarshal(byteValue, &articles)
    

    If you change that to

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

    You'll see an error being reported: xml: encoding "ISO-8859-1" declared but Decoder.CharsetReader is nil. As a best practice, you should always check for errors being returned.

    To fix that, you can either remove the encoding attribute (encoding="ISO-8859-1") from the XML or change the code of your unmarshalling a bit:

    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 < len(articles.Dblp); i++ {
            fmt.Println("Entered loop")
            fmt.Println("get title: " + articles.Dblp[i].Key)
            fmt.Println("get year: " + articles.Dblp[i].Year)
        }
    }
    
    func makeCharsetReader(charset string, input io.Reader) (io.Reader, error) {
        if charset == "ISO-8859-1" {
            // Windows-1252 is a superset of ISO-8859-1, so should do here
            return charmap.Windows1252.NewDecoder().Reader(input), nil
        }
        return nil, fmt.Errorf("Unknown charset: %s", charset)
    }
    

    running the above program results in:

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解