亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

去解析無效的 XML

去解析無效的 XML

Go
素胚勾勒不出你 2021-11-22 16:11:13
有一個指向 XML 的鏈接:http : //www.guru.com/rss/jobs/ 當嘗試使用 解析 XML 時encoding/xml,得到錯誤:第 1 行 XML 語法錯誤:無效的 XML 名稱:t我知道,此 XML 已損壞,但是我如何忽略它并解析第一項?XML 的最后一項如下所示:<item>    <title>Online Ad Posting Data Entry Jobs</t    <?xml version="1.0" encoding="utf-8"?>    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">        <channel>            <title>Guru Jobs</title>            <link>http://www.guru.com</link>            <description>Guru Jobs</description>            <lastBuildDate>Sun, 15 Nov 2015 11:04:51 GMT</lastBuildDate>            <language>en-us</language>            <atom:link href='http://www.guru.com/rss/jobs/' rel="self" type="application/rss+xml" />        </channel>    </rss>    itle>    <link>http://www.guru.com/jobs/online-ad-posting-data-entry-jobs/1189496</link>    <guid>http://www.guru.com/jobs/online-ad-posting-data-entry-jobs/1189496</guid></item> 代碼示例:type Rss2 struct {     ItemList []Item `xml:"channel>item"`}type Item struct {    Title       string      `xml:"title"`    Link        string      `xml:"link"`    Description string      `xml:"description"`    PubDate     string      `xml:"pubDate"`    GUID        string      `xml:"guid"`    }r := Rss2{}reader := bytes.NewReader(xmlRead)decoder := xml.NewDecoder(reader)decoder.CharsetReader = charset.NewReaderLabeldecoder.Strict = falseerr = decoder.Decode(&r)if err != nil { fmt.Printf(err.Error()) }
查看完整描述

2 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

XML 標簽應該正確打開和關閉。從您發布的 XML 來看,似乎 XML 聲明并非一開始。

<?xml version="1.0" encoding="utf-8"?>

這應該是一開始的。希望這可以幫助


查看完整回答
反對 回復 2021-11-22
?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

有問題的 XML 似乎是錯誤的,


這是正確版本的 XML 文件和 Go 代碼


XML文件:


<?xml version="1.0" encoding="utf-8"?>

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">

<channel>

    <title>Guru Jobs</title>

    <link>http://www.guru.com</link>

    <description>Guru Jobs</description>

    <lastBuildDate>Sun, 15 Nov 2015 11:04:51 GMT</lastBuildDate>

    <language>en-us</language>

    <atom:link href='http://www.guru.com/rss/jobs/' rel="self" type="application/rss+xml" />

    <item>

        <title>Imaging for Bespoke Curtain Website</title>

        <link>http://www.guru.com/jobs/imaging-for-bespoke-curtain-website/1203083</link>

        <guid>http://www.guru.com/jobs/imaging-for-bespoke-curtain-website/1203083</guid>

        <description><![CDATA[<b>Description:</b> Hi,We are currently developing a made to measure curtain website and are looking for help in develo...<br><b>Category:</b> Web, Software & IT<br><b>Required skills:</b> ecommerce, imaging software, opencart, web development<br><b>Fixed Price budget:</b> $500-$1k<br><b>Job type:</b> Public<br><b>Freelancer Location:</b> Worldwide<br>]]>

        </description>

        <pubDate>Mon, 04 Jan 2016 12:14:09 GMT</pubDate>

    </item>

</channel>

</rss>

示例 Go 代碼


package main


import (

    "io/ioutil"

    "encoding/xml"

    "fmt"

    )


type Rss2 struct {

    ItemList []Item `xml:"channel>item"`

}

type Item struct {

    Title       string      `xml:"title"`

    Link        string      `xml:"link"`

    Description string      `xml:"description"`

    PubDate     string      `xml:"pubDate"`

    GUID        string      `xml:"guid"`

}


func main() {

    r := Rss2{}

    xmlContent, _ := ioutil.ReadFile("example2.xml")

    if err := xml.Unmarshal(xmlContent, &r); err != nil {

        panic(err)

    }

    fmt.Println("RSS item :", r)

}

現在,您可以迭代并在 XML 中找到所需的數據。


查看完整回答
反對 回復 2021-11-22
  • 2 回答
  • 0 關注
  • 226 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號