dongyun4010 2015-03-04 15:08
浏览 18
已采纳

Golang Google警报XML解析

My XML data :

<?xml version="1.0" encoding="utf-8"?>
<feed 
    xmlns="http://www.w3.org/2005/Atom" 
    xmlns:idx="urn:atom-extension:indexing">

    <entry>
        <title type="html">Some Title</title>
        <link href="https://www.google.com"></link>
    </entry>
</feed>

I want to parse Each <entry> with its <title>& <link> tag so far I have successfully parsed the title but no success with <link>.

My Code :

type Entry struct {
    XMLName xml.Name `xml:"entry"`
    Link    string   `xml:"link"`
    Title   string   `xml:"title"`
}

type Feed struct {
    XMLName xml.Name `xml:"feed"`
    Entries []Entry  `xml:"entry"`
}

func (s Entry) String() string {
    return fmt.Sprintf("\t Link : %s - Title : %s 
", s.Link, s.Title)
}

Live demo at http://play.golang.org/p/hteQ5RuMco

  • 写回答

1条回答 默认 最新

  • dongshao1156 2015-03-04 15:27
    关注

    You're close. Your code doesn't extract the link URL because it is not the value of the element <link> but it is the value of its attribute "href".

    You can extract the value of the attribute with the following code:

    type Link struct {
        Href string `xml:"href,attr"`
    }
    

    And your modified Entry type:

    type Entry struct {
        XMLName xml.Name `xml:"entry"`
        Link    Link     `xml:"link"`
        Title   string   `xml:"title"`
    }
    

    Try your modified app on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀