fedefrappi / AePubReader

Another ePub Reader for iPad
-
369 stars 150 forks source link

Parsing OPF File #12

Open dineshprasanna opened 12 years ago

dineshprasanna commented 12 years ago

Sir, Here i have the doubt in parsing the epub file,the file listout all chapters with out any problem but if the file contains the subchapters how i list out could u help me sir. Its one of my big problem in parsing the contents of the epub file

For Sample refer the ncx file contains similar formats: 1]Historical Notes - Table of Contents 2]Florence

Form the [1] its take the title but in the [2] its not taken,how i rectify it

dineshprasanna commented 12 years ago

Is there any feedback in the above question

Rashmi1989 commented 10 years ago

Hello

  I do have the same issue.Please anyone help me regarding this.May be we have to change this method.

{ @try {

    CXMLDocument* opfFile = [[CXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:opfPath] options:0 error:nil];
    NSArray* itemsArray = [opfFile nodesForXPath:@"//opf:item" namespaceMappings:[NSDictionary dictionaryWithObject:@"http://www.idpf.org/2007/opf" forKey:@"opf"] error:nil];
    //  ////////NSLog(@"itemsArray size: %d", [itemsArray count]);

    NSString* ncxFileName;

    NSMutableDictionary* itemDictionary = [[NSMutableDictionary alloc] init];
    for (CXMLElement* element in itemsArray) {
        [itemDictionary setValue:[[element attributeForName:@"href"] stringValue] forKey:[[element attributeForName:@"id"] stringValue]];
        if([[[element attributeForName:@"media-type"] stringValue] isEqualToString:@"application/x-dtbncx+xml"]){

            ncxFileName = [[element attributeForName:@"href"] stringValue];
            //

        }

        if([[[element attributeForName:@"media-type"] stringValue] isEqualToString:@"application/xhtml+xml"]){
            ncxFileName = [[element attributeForName:@"href"] stringValue];

            //
        }
    }

    int lastSlash = [opfPath rangeOfString:@"/" options:NSBackwardsSearch].location;
    NSString* ebookBasePath = [opfPath substringToIndex:(lastSlash +1)];
    CXMLDocument* ncxToc = [[CXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%@", ebookBasePath, ncxFileName]] options:0 error:nil];
    NSMutableDictionary* titleDictionary = [[NSMutableDictionary alloc] init];
    for (CXMLElement* element in itemsArray) {
        NSString* href = [[element attributeForName:@"href"] stringValue];
        NSString* xpath = [NSString stringWithFormat:@"//ncx:content[@src='%@']/../ncx:navLabel/ncx:text", href];
        NSArray* navPoints = [ncxToc nodesForXPath:xpath namespaceMappings:[NSDictionary dictionaryWithObject:@"http://www.daisy.org/z3986/2005/ncx/" forKey:@"ncx"] error:nil];
        if([navPoints count]!=0){
            CXMLElement* titleElement = [navPoints objectAtIndex:0];
            [titleDictionary setValue:[titleElement stringValue] forKey:href];
        }
    }

    NSArray* itemRefsArray = [opfFile nodesForXPath:@"//opf:itemref" namespaceMappings:[NSDictionary dictionaryWithObject:@"http://www.idpf.org/2007/opf" forKey:@"opf"] error:nil];
    //  ////////NSLog(@"itemRefsArray size: %d", [itemRefsArray count]);
    NSMutableArray* tmpArray = [[NSMutableArray alloc] init];
    int count = 0;
    for (CXMLElement* element in itemRefsArray) {
        NSString* chapHref = [itemDictionary valueForKey:[[element attributeForName:@"idref"] stringValue]];

        Chapter* tmpChapter = [[Chapter alloc] initWithPath:[NSString stringWithFormat:@"%@%@", ebookBasePath, chapHref]
                                                      title:[titleDictionary valueForKey:chapHref]
                                               chapterIndex:count++];

        [tmpArray addObject:tmpChapter];

        [tmpChapter release];
    }

    self.spineArray = [NSArray arrayWithArray:tmpArray];

    [opfFile release];
    [tmpArray release];
    [ncxToc release];
    [itemDictionary release];
    [titleDictionary release];

}
@catch (NSException *exception)

{

}
@finally {

}

}