This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Kanna | |
struct OPFDocument { | |
let metadata: OPFMetadata | |
let manifest: OPFManifest | |
let spine: OPFSpine | |
// Other elements, like collection, guide, etc. | |
let document: XMLDocument |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class EPUBBook { | |
// ... | |
let opf: OPFDocument | |
init?(contentsOf baseURL: URL) { | |
// ... | |
// Parse the OPF file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Kanna | |
struct ContainerDocument { | |
let opfPath: String | |
let document: XMLDocument | |
init?(url: URL) { | |
do { | |
// Just one line to get the DOM document of a XML file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SAX | DOM | |
---|---|---|
Large XML file to parse or memory limitation | Small XML file or no memory limitation | |
Need to be fast at run time | The data structure is complicated | |
Only need to parse a specific piece | Need to do elements query |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Framework | Native | Processing | Query | Note | Link | |
---|---|---|---|---|---|---|
XMLParser | Y | SAX | - | Available for iOS & macOS | https://developer.apple.com/documentation/foundation/xmlparser | |
XMLDocument | Y | DOM | XPath & XQuery | Only available for macOS | https://developer.apple.com/documentation/foundation/xmldocument | |
libxml2 | Y | SAX & DOM | XPath & CSS Selector | C library | http://xmlsoft.org/ | |
Kanna | N | SAX & DOM | XPath & CSS Selector | Built on libxml2 | https://github.com/tid-kijyun/Kanna | |
SWXMLHash | N | DOM | Hash Query | Built on XMLParser | https://github.com/drmohundro/SWXMLHash | |
XMLParsing | N | DOM | Decoder | Built on XMLParser | https://github.com/ShawnMoore/XMLParsing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Kanna | |
struct OPFSpine { | |
private(set) var idrefs = [String]() | |
init?(package: XMLElement) { | |
guard let spine = package.at_xpath("opf:spine", namespaces: XPath.opf.namespace) else { return nil } | |
idrefs = spine.xpath("opf:itemref[@idref]/@idref", namespaces: opfNamespace) | |
.map { $0.text } | |
.compactMap { $0 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<package xmlns="http://www.idpf.org/2007/opf" dir="ltr" prefix="se: http://standardebooks.org/vocab/1.0" unique-identifier="uid" version="3.0" xml:lang="en-US"> | |
<spine toc="ncx"> | |
<itemref idref="titlepage.xhtml"/> | |
<itemref idref="chapter-1.xhtml"/> | |
<itemref idref="chapter-2.xhtml"/> | |
<itemref idref="chapter-3.xhtml"/> | |
<itemref idref="uncopyright.xhtml"/> | |
<!--Other itemrefs--> | |
</spine> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Kanna | |
struct OPFManifest { | |
private(set) var items = [String: ManifestItem]() | |
init?(package: XMLElement) { | |
let opfNamespace = ["opf": "http://www.idpf.org/2007/opf"] | |
guard let manifest = package.at_xpath("opf:manifest", namespaces: opfNamespace) else { return nil } | |
let itemElements = manifest.xpath("opf:item", namespaces: opfNamespace) | |
for itemElement in itemElements { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<package xmlns="http://www.idpf.org/2007/opf" dir="ltr" prefix="se: http://standardebooks.org/vocab/1.0" unique-identifier="uid" version="3.0" xml:lang="en-US"> | |
<manifest> | |
<item href="toc.ncx" id="ncx" media-type="application/x-dtbncx+xml"/> | |
<item href="text/uncopyright.xhtml" id="uncopyright.xhtml" media-type="application/xhtml+xml"/> | |
<item href="toc.xhtml" id="toc.xhtml" media-type="application/xhtml+xml" properties="nav"/> | |
<item href="text/chapter-2.xhtml" id="chapter-2.xhtml" media-type="application/xhtml+xml"/> | |
<item href="images/cover.jpg" id="cover.jpg" media-type="image/jpeg" properties="cover-image"/> | |
<item href="text/chapter-3.xhtml" id="chapter-3.xhtml" media-type="application/xhtml+xml"/> | |
<item href="images/titlepage.png" id="titlepage.png" media-type="image/png"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Kanna | |
struct OPFMetadata { | |
// DCMES Required Elements | |
private(set) var identifiers = [String]() | |
private(set) var titles = [String]() | |
private(set) var languages = [String]() | |
// DCMES Optional Elements | |
private(set) var creators = [String]() | |
private(set) var date: String? |
NewerOlder