从 google api 解析 xml 数据

标签 parsing

我有一个谷歌地图链接,它返回一个 xml 给我。我想要该 xml 中特定标记的值。有人可以建议我该怎么做吗?链接:

http://maps.google.com/maps/api/geocode/xml?address=1270 Broadway Ste 803, New York, NY 10001, USA&sensor=false

最佳答案

这是一个返回字典中坐标的函数。注意:这是新的 V3 api。无需 API key 。传感器是。

    // Figure out the geocoordinates for the location
private Dictionary<string, string> GeoCode(string location)
{
    // Initialize the dictionary with empty values.
    Dictionary<string, string> dic = new Dictionary<string, string>();
    dic.Add("latitude", "");
    dic.Add("longitude", "");

    try
    {
        string url = "http://maps.google.com/maps/api/geocode/xml?address=" + System.Web.HttpUtility.UrlEncode(location) + "&sensor=false";
        XmlDocument doc = new XmlDocument();
        doc.Load(url);
        XmlNodeList nodes = doc.SelectNodes("//location");

        // We're assuming there's only one location node in the xml.
        foreach (XmlNode node in nodes)
        {
            dic["latitude"] = node.SelectSingleNode("lat").InnerText;
            dic["longitude"] = node.SelectSingleNode("lng").InnerText;
        }
    }
    catch (Exception)
    {
        // If anything goes wrong, we want to get empty values back.
        dic["latitude"] = "";
        dic["longitude"] = "";
    }

    return dic;
}

关于从 google api 解析 xml 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2558810/

相关文章:

c - 使用 C/C++ 解析电子邮件 header 字段

parsing - ANTLR 是序列化/反序列化二进制数据格式的合适工具吗?

解析算术表达式内的表达式

c# - 解析具有动态列数的大型分隔文件

javascript - 如何解析动态变化的HTML?

c++ - 阅读多行,但特别是......有效地解析它们

c - 从 HTML 文件中提取纯文本

java - Java 中的 RSS 提要解析器库

python - 使用 Python 从 HTML 中提取字符串不适用于正则表达式或 BeautifulSoup

ios - 在 Objective-C 中解析 JSON