AePubReader icon indicating copy to clipboard operation
AePubReader copied to clipboard

Parsing OPF File

Open dineshprasanna opened this issue 12 years ago • 2 comments

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]<navPoint id="id02-content.htm" playOrder="3"><navLabel>Historical Notes - Table of Contents</navLabel></navPoint> 2]<navPoint id="florence" playOrder="4"><navLabel>Florence</navLabel>

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

dineshprasanna avatar Mar 15 '12 07:03 dineshprasanna

Is there any feedback in the above question

dineshprasanna avatar Mar 28 '12 08:03 dineshprasanna

Hello

  I do have the same issue.Please anyone help me regarding this.May be we have to change this method.
  • (void) parseOPF:(NSString*)opfPath

{ @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 {





}

}

Rashmi1989 avatar Dec 19 '13 08:12 Rashmi1989