Ошибка демаршалинга простого XML в Golang - PullRequest
0 голосов
/ 28 февраля 2019

Я пытаюсь написать очень простой парсер в Go для большого XML-файла ( dblp.xml ), фрагмент которого приведен ниже:

<?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код выглядит следующим образом, и похоже, что что-то происходит на 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 < 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 Ответ

0 голосов
/ 28 февраля 2019

В вашем коде есть конкретная строка, которая возвращает ошибку

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 < 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)
}

запуск вышеуказанной программы приводит к:

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
...