移动设备和平台吹嘘每个新版本都有更多特性和功能,并且仅仅数月重要通告就从主流移动设备提供商中分离出来了。标题多数是关于 UI 特性(例如先进的多点触摸功能和 Adobe? Flash? 技术)以及硬件提升(例如,处理器速度和存储容量)。但是至关重要的仍然是 “内容为王”。内容 — 或者,一般来说,是数据 — 在应用程序、服务器、移动设备和用户之间不停地被交换。智能手机,如 Apple 的 iPhone 和 Google 的 Android 只是标价过高且表现不佳的移动电话,而且也不能很好的工作 。 常用缩略语
考虑到社会网络非比寻常的成功,例如:Facebook、LinkedIn 和 Twitter。单从特征和功能的角度来说,这些平台很大程度上都是单调的,之所以流行是因为会员和网站访问者可以从其公布的内容获益。而且,其内容逐渐可以通过移动设备访问。 这篇文章主要说明 Android 平台上 XML 和 JSON 数据交换格式的使用。对于一个 Twitter 帐户,示例应用程序数据源是一个状态更新 feed。Feed 数据可以以 XML 和 JSON 格式从 Twitter 上获取。正如您所看到的,操作数据的程序设计方法在这两种格式中明显不同。 建议您安装 Android SDK version 1.5 或最新版以及 Eclipse 来运行本文附带的 示例代码。为了了解更多关于设置环境的知识,访问 Android Developers 网站。使用一个活动的帐户跟着示例练习是很重要的,但不是必须的。相关链接见 参考资料。 首先我们来简要的看一下这两个数据格式,从 XML 开始。如果您对 XML 和 JSON 已经很熟悉了,就可以跳转到 应用机会:Twitter feeds 在 Android 上开始运行。 XML:一个老朋友不再是苦役当您将 XML 与 XML 普遍应用之前的工艺水平进行比较时,XML 的自述特性是很清楚的。此外,数据交换练习包括繁琐的数据描述文档,通常是在一个文字处理器或电子表格应用程序中手工编写的。这些文档,通常称为接口规范,描述了字段名、长度、分隔符、层次结构等等。用户采用他们认为合适的实践;最接近标准的是熟悉的逗号分隔值(CSV)模式,尽管 CSV 文件变化很大。如果您怀疑这一点,试着将一个 CSV 文件导入电子表格应用程序,并且注意所有可用选项。 事实上,近几年来为企业、web 或移动市场编程的人都遇到过 XML,几乎您见到的每个地方都有。 一个 XML 文档有一个可辨认的结构:一系列可以随意包含属性和子元素的元素。每个有效的 XML 文件第一行都有这样一个声明: XML 模式尽管 XML 文档是自述的,但也必须遵循一定的规则和指导方针,这就需要 XML 模式,它是一个描述特定 XML 文件结构的文档。此类结构通常是冗长且复杂的。(值得争议的是,XML 对 IT 领域最糟糕的贡献是,当高度描述的数据结构这一理念成为时尚时导致了数据爆炸,且这种情况由于过去十年中磁盘存储技术成本大幅降低而加剧。) 当这些大且复杂的文件变得更规范时,对程序员和分析员来说手工处理通常是不可能完成的。为了解决这个问题,XML 编辑器和验证工具可以在文件和相关的任务管理方面给予帮助。例如,文档和遗留格式转换。 除了一般的文本数据,XML 过去常用于存储二进制数据,通过一个称为 通过使用 XML 作为一个结构来执行请求/响应查询,这并不是 API 使用该功能的常用方式。响应数据经常包含一个包含在 清单 1. 将数据封装到 XML 结构中并将其置于响应元素中<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <request> <query> <lastname>Mott</lastname> <maxhits>100</maxhits> </query> </request> <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <response> <returncode>200</returncode> <query> <lastname>Mott</lastname> <hits>1</hits> </query> <data> <![CDATA[ <contact> <firstname>Troy</firstname> <lastname>Mott</lastname> <age>not telling</age> </contact> ]]> </data> </response> 工作空间中的 XML今天,XML 是默认的、预定数据格式。尽管同一数据的其他格式也可用,但基于 XML 结构的可用性进行计划是一种最安全的方式。 Enterprise Resource Planning (ERP) 包频繁使用 XML 进行数据到任务的导入和导出。Internet 新闻网站经常将数据提供为 Really Simple Syndication (RSS) — 具有新闻阅读软件可以处理的预定义格式的 XML 文档。甚至文字处理应用程序(例如 OpenOffice.org 和 Microsoft? Office)也使用 XML。 现在的 Microsoft Office 文件是 PKZIP 兼容的,含有多个 XML 文档。 每个 XML 在第一行都有这个常见声明。正如您在 清单 2 所看到的,阅读这些属性可能有点难: 清单 2. 每个文件第一行的常见声明<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <w:document xmlns:ve="http://schemas./markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas./officeDocument/2006/relationships" xmlns:m="http://schemas./officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas./drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas./wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"> <w:body><w:p w:rsidR="00B6337C" w:rsidRDefault="00663F0E"><w:r> <w:t xml:space="preserve">This is a sample </w:t></w:r><w:r w:rsidRPr="006906EA"><w:rPr><w:i/></w:rPr><w:t>Microsoft Word document</w:t></w:r><w:r><w:t xml:space="preserve"> used to </w:t></w:r><w:r w:rsidRPr="006906EA"><w:rPr><w:b/> <w:u w:val="single"/></w:rPr><w:t>demonstrate</w:t></w:r> <w:r><w:t xml:space="preserve"> some XML topics.</w:t></w:r> </w:p><w:p w:rsidR="00B14B2A" w:rsidRDefault="00B14B2A"/><w:p w:rsidR="00B14B2A"w:rsidRDefault="00B14B2A"><w:r><w:rPr> <w:noProof/></w:rPr><w:drawing><wp:inline distT="0" distB="0" distL="0" distR="0"><wp:extent cx="3276600" cy="3838575"/><wp:effectExtent l="19050" t="0" r="0" b="0"/><wp:docPr id="1" name="Picture 0" descr="frankableson.jpg"/><wp:cNvGraphicFramePr><a:graphicFrameLocks xmlns:a="http://schemas./drawingml/2006/main" noChangeAspect="1"/></wp:cNvGraphicFramePr><a:graphic xmlns:a="http://schemas./drawingml/2006/main"><a:graphicData uri="http://schemas./drawingml/2006/picture"><pic:pic xmlns:pic="http://schemas./drawingml/2006/picture"> <pic:nvPicPr><pic:cNvPrid="0"name="frankableson.jpg"/><pic:cNvPicPr/> </pic:nvPicPr><pic:blipFill><a:blip r:embed="rId4" cstate="print"/><a:stretch><a:fillRect/></a:stretch> </pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0"/> <a:ext cx="3276600" cy="3838575"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr></pic:pic> </a:graphicData></a:graphic></wp:inline></w:drawing> </w:r></w:p><w:p w:rsidR="00663F0E" w:rsidRDefault="00663F0E"/> <w:p w:rsidR="00CC16CE" w:rsidRDefault="00CC16CE"/><w:sectPr w:rsidR="00CC16CE" w:rsidSect="00B6337C"><w:pgSz w:w="12240" w:h="15840"/> <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:space="720"/><w:docGrid w:linePitch="360"/></w:sectPr></w:body></w:document> XML 是自述的,但是并不意味着标记一定是容易理解的。这个虚构的示例展示了多个名称空间的使用,多个名称使得理解 XML 文件更有挑战性,如果不使用专用工具就很难理解。 XML 随处可见,但是对于 Android 程序员来说并不是一个明智的选择,特别是,如果数据结构成为与 XML 结构伴随着的数据爆炸的牺牲品。像 Android 这样通常在蜂窝数据网络(cellular data network)上工作的资源受限平台不能存储和解析大量 XML 数据。然而,如果特殊编程任务要求文本和二进制数据交换,那么 XML 是一个可靠的选择。 现在,让我们看一看数据交换的另一个格式:JSON。 JSON:网络的新成员越来越多的 Internet API 供应商提供 JSON 作为一种数据格式。JSON 在 Ajax (Asynchronous JavaScript and XML) 网页编程社区中享有盛名。Ajax 技术使 web 页面可以动态地更新,只更新所选区域的数据而不用更新整个页面。由于较少数据被传送 — 更重要的是,因为较少的数据被解析且显示在浏览器窗口 — 使用 AJAX 的应用程序比起传统 web 应用程序能够提供一个更好的用户体验。事实上,一个结构良好的 Ajax 应用程序可与智能或富客户端应用程序相抗衡。 当 Ajax 应用程序与 web 服务器交换数据时,经常需要某类数据的刷新,但不需要格式化。通常认为一个 web 服务器提供预格式化(preformatted)HTML 是一个糟糕的实践。相反的,一个格式良好的应用程序应该将数据内容发送到浏览器并应用一个 Cascading Style Sheets (CSS) 文件来产生视觉效果,比如颜色和特殊字体。 假设应用程序想要请求 Mr. Mott.(这是我们虚构的)的联系记录,应用程序返回浏览器的数据元素不止一个。它是如何包装的呢?在 清单 1 示例中,您可以使用一个简单的请求/响应结构。这已经足够了;然而它要求您解析来自服务器的每个响应、以某种结构(DOM)存储数据,然后更新网页内容。 还有一种选择,您可以从服务器上返回一些 JavaScript,并用它来直接处理。以下是一个虚拟应用程序的样例响应,对 Mott 查询( [{"firstname":"Troy","lastname":"Mott","age":"don't ask!"},{"firstname":"Apple seed", "lastname":"Mott's","age":"99"}] XML 以其冗长而著名,而 JSON 也因其难度而闻名。JSON 对象其构造是一个键 :值 对,对象元素之间用逗号隔开,每个对象被包含在一对大括号 清单 3 显示了一个在 HTML 页面中使用这类对象的示例。为简单起见,不包括服务器通信;相反,JSON 数据作为一个字符串变量 清单 3. 在 HTML 页面中使用一个 JSON 对象<html> <head> <script language="JavaScript"> var serverresponse = "[{\"firstname\":\"Troy\",\"lastname\":\"Mott\",\"age\":\"don't ask!\"},{\"firstname\":\"Apple seed\",\"lastname\":\"Mott's\",\"age\":\"99\"}]"; function updatepage() { var contacts = eval(serverresponse ); var i; var s = "Search Results:<br />"; for (i=0;i<contacts.length;i++) { s = s + contacts[i].firstname + " " + contacts[i].lastname + "'s age is ... " + contacts[i].age + "<br />"; } document.getElementById("target").innerHTML = s; } </script> </head> <body> <button onclick="updatepage();">Search for Mott</button><br /> <span id="target"> </span> </body> </html> 注意,这个示例使用 JavaScript 函数 总的来说,JSON 是:
在 清单 3 中,客户端是一个运行客户端脚本的 web 浏览器。返回本文主题,接下来将研究在 Android 应用程序中 XML 和 JSON 的使用。 应用机会:Twitter feedsTwitter 现在已经变成了一种国际力量,它提供的更新包罗万象:从什么人正在吃早饭,他们的孩子的球队在棒球比赛中的表现这类琐事,到封闭国家中的政治动乱的街头新闻,器官移植实时报道等严肃话题。 要获取一些 XML 和 JSON 文档以用于本文附带的 样例代码,最简单的方法就是通过 URL http://twitter.com/statuses/user_timeline/userid.format,其中 userid 是您的 Twitter 用户 ID,format 是您要的格式:XML 或 JSON。 您也可以直接在您的 Twitte 页面上找到此页的链接,如 图 1 所示。在那里可以看到您的 Twitter 用户 ID。 图 1. 您 Twitter 页面上的 feed 页链接完整的 feed 文件相当冗长,下面两个清单只显示 feed 的第一项(来自我的 Twitter 帐户)。清单 4 包含以下 XML 片段: 清单 4. XML 片段<?xml version="1.0" encoding="UTF-8"?> <statuses type="array"> <status> <created_at>Thu Apr 29 05:25:29 +0000 2010</created_at> <id>13052369631</id> <text>Wrapping up new article on JSON for Android programmers...</text> <source><a href="http://www.linkedin.com/"rel="nofollow"> LinkedIn</a></source> <truncated>false</truncated> <in_reply_to_status_id/> <in_reply_to_user_id/> <favorited>false</favorited> <in_reply_to_screen_name/> <user> <id>15221439</id> <name>fableson</name> <screen_name>fableson</screen_name> <location>Byram Township, NJ</location> <description/> <profile_image_url>http://a3./profile_images/260492935 /bookcover_normal.jpg</profile_image_url> <url>http://</url> <protected>false</protected> <followers_count>52</followers_count> <profile_background_color>9ae4e8 <profile_text_color>000000</profile_text_color> <profile_link_color>0000ff</profile_link_color> <profile_sidebar_fill_color>e0ff92 </profile_sidebar_fill_color> <profile_sidebar_border_color>87bc44 </profile_sidebar_border_color> <friends_count>10</friends_count> <created_at>Tue Jun 24 17:04:11 +0000 2008</created_at> <favourites_count>0</favourites_count> <utc_offset>-18000</utc_offset> <time_zone>Eastern Time (US & Canada)</time_zone> <profile_background_image_url>http://s./a/1272044617/ images/themes/theme1/bg.png</profile_background_image_url> <profile_background_tile>false</profile_background_tile> <notifications>false</notifications> <geo_enabled>false</geo_enabled> <verified>false</verified> <following>false</following> <statuses_count>91</statuses_count> <lang>en</lang> <contributors_enabled>false</contributors_enabled> </user> <geo/> <coordinates/> <place/> <contributors/> </status> </statuses> 清单 5 是同一数据,这次是以 JSON 格式显示: 清单 5. JSON 格式的 feed 数据[ {"in_reply_to_status_id":null, "favorited":false, "created_at":"Thu Apr 29 05:25:29 +0000 2010", "in_reply_to_screen_name":null, "geo":null, "source":"<a href=\"http://www.linkedin.com/\" rel=\"nofollow ">LinkedIn</a>", "contributors":null, "place":null, "truncated":false, "coordinates":null, "user": { "friends_count":10, "description":"", "lang":"en", "statuses_count":91, "time_zone":"Eastern Time (US & Canada)", "profile_link_color":"0000ff", "favourites_count":0, "created_at":"Tue Jun 24 17:04:11 +0000 2008", "contributors_enabled":false, "profile_sidebar_fill_color":"e0ff92", "following":null, "geo_enabled":false, "profile_background_image_url":"http://s./a/1272044617/images/themes /theme1/bg.png", "profile_image_url":"http://a3./profile_images/260492935 /bookcover_normal.jpg", "notifications":null, "profile_sidebar_border_color":"87bc44", "url":"http://", "verified":false, "profile_background_tile":false, "screen_name":"fableson", "protected":false, "location":"Byram Township, NJ", "profile_background_color":"9ae4e8", "name":"fableson", "followers_count":52, "id":15221439, "utc_offset":-18000, "profile_text_color":"000000" }, "in_reply_to_user_id":null, "id":13052369631, "text":"Wrapping up new article on JSON for Android programmers..."} ] 注意,在两个清单中除了状态更新外还有非常多附加数据。您只需要注意日期/时间,何时发布公告以及公告的内容。接下来,将向您展示解析该数据的 Android 应用程序的相关部分。整个项目见 下载 部分。 XMLvsJSON 应用程序真实的实时更新需要注意的是,样例应用程序没有实时地从 web 移动数据,尽管我们期盼一个真实应用程序。由于数据 feeds 取自原始资源文件夹,因此应用程序关注的是分析方面。关于使用 Android 进行网络连接相关信息的链接,见 参考资料。 Android 应用程序很简单。其中包含 XML 和 JSON 数据资料的全部副本。用户可以任选其一进行分析。 图 2 显示 Eclipse 中项目文件的结构。(查看 图 2 的文本版本。) 图 2. Eclipse 项目的文件结构![]() 图 3 显示选择解析选项之前的应用程序 UI: 图 3. 选择解析选项之前的应用程序 UI![]() 应用程序 UI 有两个按钮,Parse XML 和 Parse JSON,接着是默认文本。清单 6 包含该 UI 的布局,在项目的 res/layout 文件夹中的 main.xml 中可以找到: 清单 6. UI 的布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas./apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout xmlns:android="http://schemas./apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnXML" android:text="Parse XML"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnJSON" android:text="Parse JSON file"></Button> </LinearLayout> <ScrollView xmlns:android="http://schemas./apk/res/android" android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="default text" android:layout_gravity="center_horizontal" android:id="@+id/txtData" /> </ScrollView> </LinearLayout> Parse XML 和 Parse JSON file 按钮是在 注意多个 清单 7. |
描述 | 名字 | 大小 |
---|---|---|
本文源代码 | xmlvsjosn.zip | 68KB |
|
来自: 气自华511 > 《XML和JSON》