英文:
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!
<?xml version="1.0" encoding="utf-8"?>
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论