首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

用 Exhibit 真正链接开放数据(2)从 Atom 到 JSON

用 Exhibit 真正链接开放数据(2)从 Atom 到 JSON

从 Atom 到 JSON有很多方式可以将 Atom 1.0 转变成 JSON,为此,已经有人编写了多个质量各异的 XSLT 脚本。我通常习惯使用 Python 代码,但幸运的是能够将非常友好的处理 XML 的库和处理 JSON 的库结合起来。前者是 Amara XML Toolkit,后者是 simplejson。是完整的脚本 (atom2json.py),用来从 所示的 Atom 生成 Exhibit JSON。
清单 3 (atom2json.py). 将 Atom 转变成 Exhibit JSON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
               import sys
import amara
import simplejson

#Parse Atom XML from the file name or URL given on the command line
source = sys.argv[1]
doc = amara.parse(source)

#This is a list comprehension.  It iterates over the list of entry elements
#And creates a Python dictionary for each, with keys and values drawn from
#Each entry.
entries = [
   #Result spec of the list comprehension
   {
       u"label": unicode(e.id),
       u"type": u"Entry",
       u"title": unicode(e.title),
       #Nested list comprehension to select the alternate link,
       #then select the first result ([0]) and gets its href attribute
       u"link": [ l for l in e.link if l.rel == u"alternate" ][0].href,
       u"author": unicode(e.author.name),
       u"logo": unicode(e.logo),
       #Nested list comprehension to create a list of category values
       u"categories": [ unicode(c.term) for c in e.category ],
       u"updated": unicode(e.updated),
       u"content": unicode(e.content),
   }
   #Iteration spec of the list comprehension
   for e in doc.feed.entry
]

exhibit_obj = {'items': entries}

#Write the result as JSON to the console
simplejson.dump(exhibit_obj, sys.stdout, indent=4)




Amara 允许以一种很简单的方式访问 XML 文档,就如同这类文档是 Python 的一种结构一样(它也试图通过保留 XML 的基本特征来在二者之间取得均衡)。simplejson 则允许使用 Python 中的等效工具来读写 JSON,例如,用一个 Python 列表来表示 JSON 数组,或一个 Python dictionary(关联数组或哈希表类型)。(feed.js) 是 的运行结果,清单 3 可以这么调用:python atom2json.py clipart.feed.xml > feed.js。您可以使用任何自己喜欢的工具和技术来生成 JSON,这样一来,您就能尽享其中的快乐了。   
清单 4 (feed.js). 由清单 3 创建的 Exhibit JSON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
    "items": [
        {
            "updated": "2008-03-11T20:06:06+00:00",
            "title": "Hard Hat",
            "author": "Adriano Ribeiro",
            "label": "http:\/\/openclipart.org\/media\/files\/adriano\/8006",
            "content": "A yellow hard hat and a wrench",
            "link": "http:\/\/openclipart.org\/media\/files\/adriano\/8006",
            "logo": "http:\/\/openclipart.org\/people\/adriano\/avatar.gif",
            "type": "Entry",
            "categories": [
                "unchecked",
                "public_domain",
                "image",
                "tool",
                "man",
                "hat",
                "hard",
                "work",
                "wrench"
            ]
        },
        {
            "updated": "2008-03-11T16:45:01+00:00",
            "title": "Ship's Badge",
            "author": "Caggles",
            "label": "http:\/\/openclipart.org\/media\/files\/Caggles\/8005",
            "content": "Blank outline for a Royal Navy ship's crest. \n      ",
            "link": "http:\/\/openclipart.org\/media\/files\/Caggles\/8005",
            "logo": "http:\/\/openclipart.org\/ccimages\/person.png",
            "type": "Entry",
            "categories": [
                "unchecked",
                "public_domain",
                "image",
                "svg",
                "png",
                "badge",
                "heraldry",
                "crest",
                "navy",
                "ship",
                "insignia"
            ]
        }
    ]
}

返回列表