Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing OPF File #12

Open
dineshprasanna opened this issue Mar 15, 2012 · 2 comments
Open

Parsing OPF File #12

dineshprasanna opened this issue Mar 15, 2012 · 2 comments

Comments

@dineshprasanna
Copy link

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
Copy link
Author

Is there any feedback in the above question

@Rashmi1989
Copy link

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 {





}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants