Golang XML解析问题

huangapple go评论78阅读模式
英文:

Golang XML Parsing issue

问题

我刚刚开始尝试使用谷歌的Go(Golang)语言,并遇到了一个我想高效解决的问题。我想从一个在线可用的XML文件中提取一些数据(我将其作为轮询会话的响应获取),但我不知道如何入手。我应该下载页面的源代码,将其存储在本地并从中提取数据,还是有可能在不下载整个页面的情况下提取数据?谢谢!

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

I have just started experimenting with Google's GO (Golang) language, and have run into a problem that I would like to solve efficiently.
So I would like to extract some data from an XML file, that is available online (I get it as the response of a polling session), but I don't know how to get started. Should I download the source code of the page, store it locally and extract the data that way or is there a possibility to extract the data without downloading the whole thing? Thank you!

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;

答案1

得分: 2

由于它可以在线使用,您可以使用net/http客户端来获取XML。然后,您可以使用encoding/xml包将响应体转换为结构体对象,它具有xml.Unmarshal方法将XML字符串转换为结构体 - 因此您应该先编写结构体。

net/http示例可在以下链接找到:

encoding/xml示例可在以下链接找到:

英文:

Since its available online you can use net/http client to retrieve the xml. Then you can use encoding/xml package and convert the response body to struct object, it has xml.Unmarshal method to convert xml string to struct - hence you should write the struct first.

net/http example available

encoding/xml example

huangapple
  • 本文由 发表于 2016年10月2日 07:09:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/39812310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定