第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Golang 中解組一個簡單的 xml 時出錯

在 Golang 中解組一個簡單的 xml 時出錯

Go
犯罪嫌疑人X 2023-06-12 15:04:38
我正在嘗試在 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>我的代碼如下,看起來好像在 發(fā)生了一些事情xml.Unmarshal(byteValue, &articles),因為我無法在輸出中獲取任何 xml 的值。你能幫我解決我的代碼有什么問題嗎?
查看完整描述

1 回答

?
青春有我

TA貢獻1784條經(jīng)驗 獲得超8個贊

您的代碼中有特定行返回錯誤


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。作為最佳實踐,您應(yīng)該始終檢查返回的錯誤。


要解決此問題,您可以從 XML 中刪除編碼屬性 ( encoding="ISO-8859-1") 或稍微更改解組代碼:


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


查看完整回答
反對 回復 2023-06-12
  • 1 回答
  • 0 關(guān)注
  • 101 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號