Skip to content

Commit 80ad724

Browse files
author
Andrew Eiche
committed
Namespaces in XML... UGH!!!
1 parent a96d34f commit 80ad724

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

UnityEPubReader/Assets/Scripts/EbookRenderer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ void OpenEbookFile(){
2121
//displayText.text = chapter.HtmlContent;
2222
var epub = new UEPubReader ("Assets/Books/austen-pride-and-prejudice-illustrations.epub");
2323
Debug.Log (epub.epubFolderLocation);
24+
25+
displayText.text = epub.chapters[10];
2426
}
2527
}

UnityEPubReader/Assets/Scripts/UEPubReader.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
using System.Xml;
55
using System.IO;
66
using System.Text;
7+
using System.Linq;
78

89
namespace UEPub{
910
public class UEPubReader {
1011

1112
public string epubFolderLocation { get; private set; }
1213

14+
public List<string> chapters;
15+
1316
private Dictionary<string, string> bookItems;
1417
private List<string> spine;
1518

@@ -32,6 +35,9 @@ public UEPubReader(string file){
3235
epubFolderLocation = Application.temporaryCachePath + "/" + folderName;
3336

3437
ZipUtil.Unzip ( file, epubFolderLocation);
38+
39+
ParseContainer ();
40+
ParseOPF ();
3541
}
3642

3743
private void ParseContainer(){
@@ -40,9 +46,11 @@ private void ParseContainer(){
4046
XmlDocument doc = new XmlDocument ();
4147
doc.Load (containerFile);
4248

43-
XmlNode rootFile =
44-
doc.SelectSingleNode("/container/rootfiles/rootfile");
45-
opfFileString = rootFile.Attributes ["full-path"].InnerText;
49+
var xmlnsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
50+
xmlnsManager.AddNamespace("ns", "urn:oasis:names:tc:opendocument:xmlns:container");
51+
52+
XmlNodeList rootFiles = doc.SelectNodes("/ns:container/ns:rootfiles/ns:rootfile", xmlnsManager);
53+
opfFileString = rootFiles[0].Attributes ["full-path"].InnerText;
4654
}
4755

4856
private void ParseOPF(){
@@ -51,17 +59,33 @@ private void ParseOPF(){
5159
XmlDocument doc = new XmlDocument ();
5260
doc.Load (opfFile);
5361

54-
var nodes = doc.SelectNodes ("/package/manifest");
62+
var xmlnsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
63+
xmlnsManager.AddNamespace("ns", "http://www.idpf.org/2007/opf");
64+
xmlnsManager.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
65+
xmlnsManager.AddNamespace("dcterms", "http://purl.org/dc/terms/");
66+
67+
var nodes = doc.SelectNodes ("/ns:package/ns:manifest/ns:item", xmlnsManager);
5568

5669
foreach (XmlNode node in nodes) {
5770
bookItems[node.Attributes["id"].InnerText] = node.Attributes["href"].InnerText;
5871
}
5972

60-
nodes = doc.SelectNodes("/package/spine");
73+
nodes = doc.SelectNodes("/ns:package/ns:spine/ns:itemref", xmlnsManager);
6174

6275
foreach (XmlNode node in nodes){
6376
spine.Add(node.Attributes["idref"].InnerText);
6477
}
78+
79+
var toc = spine.Select (x => bookItems [x])
80+
.ToList ();
81+
82+
chapters = new List<string> ();
83+
84+
foreach (string s in toc) {
85+
StreamReader sr = new StreamReader(Path.GetDirectoryName(opfFile) + "/" + s);
86+
chapters.Add(sr.ReadToEnd());
87+
sr.Close();
88+
}
6589
}
6690
}
6791
}

0 commit comments

Comments
 (0)