阅读 ePub 格式 [英] Reading ePub format

查看:31
本文介绍了阅读 ePub 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个 iPhone 应用程序来读取 ePub 文件.有没有可用的框架来开发这个?我不知道如何阅读这种文件格式.我尝试使用 NSXML Parser 解析扩展名为 .epub 的示例文件,但失败了.

解决方案

EPUB 格式汇集了一堆不同的规范/格式:

  • 说明这本书的内容应该是什么样子的(XHTML 1.1 + CSS 的一个子集)
  • 定义一个清单",列出构成该内容的所有文件(OPF,这是一个 XML 文件)
  • 定义所有内容的打包方式(OEBPS:清单中所有内容的 zip 文件以及一些额外文件)

规范看起来有点令人生畏,但实际上,一旦您掌握了基础知识(解压缩、解析 XML),它就不会特别困难或复杂.

您需要弄清楚如何下载 EPUB、将其解压缩到某处、解析清单,然后显示相关内容.

如果您刚刚开始,请提供一些建议:

要显示内容,现在只需使用 UIWebView.

以下是代码的高级分步说明:

1) 创建一个带有 UIWebView

的视图

2) 下载 EPUB 文件

3) 使用上面链接的 zip 库将其解压缩到应用程序文档文件夹中的子目录

4) 使用上面链接的 TBXML 解析 META-INF/container.xml 中的 XML 文件(如果此文件不存在,则 EPUB 无效)

5) 在此 XML 中,找到媒体类型为 application/oebps-package+xml 的第一个根文件".这是本书的 OPF 文件.

6) 解析 OPF 文件(也是 XML)

7) 现在你需要知道这本书的第一章是什么.

a) 元素中的每个 都有一个 id 和一个 href.将它们存储在 NSDictionary 中,其中键是 id,对象是 href.

b) 查看 中的第一个 .它有一个 idref 属性,它对应于 (a) 中的一个 id.在 NSDictionary 中查找该 ID,您将获得一个 href.

c) 这是第一章的文件给用户看.计算出完整路径是什么(提示:它是您将 zip 文件解压到 (3) 中的任何位置以及 (6) 中 OPF 文件的基本目录)

8) 使用 fileURLWithPath: 创建一个 NSURL,其中路径是 (7c) 中的完整路径.使用您在 (1) 中创建的 UIWebView 加载此请求.

您需要实现前进/后退按钮或滑动或其他东西,以便用户可以从一章移动到另一章.使用 确定接下来要显示的文件 - XML 中的 是按照它们应该出现在读者面前的顺序.>

I am trying to develop an iPhone application to read ePub files. Is there any framework available to develop this? I have no idea about how to read this file format. I tried to parse a sample file with .epub extension using NSXML Parser, but that fails.

解决方案

The EPUB format brings together a bunch of different specifications / formats:

  • one to say what the content of the book should look like (a subset of XHTML 1.1 + CSS)
  • one to define a "manifest" that lists all of the files that make up that content (OPF, which is an XML file)
  • one to define how everything is packaged up (OEBPS: a zip file of everything in the manifest plus a few extra files)

The specs look a bit daunting but actually once you've got the basics (unzipping, parsing XML) down it's not particularly difficult or complex.

You'll need to work out how to download the EPUB, to unzip it somewhere, to parse the manifest and then to display the relevant content.

Some pointers if you're just starting out:

To display content just use a UIWebView for now.

Here's a high level step by step for your code:

1) create a view with a UIWebView

2) download the EPUB file

3) unzip it to a subdirectory in your app's documents folder using the zip library, linked above

4) parse the XML file at META-INF/container.xml (if this file doesn't exist the EPUB is invalid) using TBXML, linked above

5) In this XML, find the first "rootfile" with media-type application/oebps-package+xml. This is the OPF file for the book.

6) parse the OPF file (also XML)

7) now you need to know what the first chapter of the book is.

a) each <item> in the <manifest> element has an id and an href. Store these in an NSDictionary where the key is the id and the object is the href.

b) Look at the first <itemref> in the <spine>. It has an idref attribute which corresponds to one of the ids in (a). Look up that id in the NSDictionary and you'll get an href.

c) this is the the file of the first chapter to show the user. Work out what the full path is (hint: it's wherever you unzipped the zip file to in (3) plus the base directory of the OPF file in (6))

8) create an NSURL using fileURLWithPath:, where the path is the full path from (7c). Load this request using the UIWebView you created in (1).

You'll need to implement forward / backward buttons or swipes or something so that users can move from one chapter to another. Use the <spine> to work out which file to show next - the <itemrefs> in the XML are in the order they should appear to the reader.

这篇关于阅读 ePub 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆