1

我是 iPhone 开发者的新手,

我为我的应用程序制作了 epub 阅读器,我想在其中<text>从本地页面获取标记的所有值,.html并将其存储在我的数组中。

例如: 这是我的toc.html文件,我想从这个文件中获取<text>标签的所有值,

<navMap>
    <navPoint class="chapter" id="navpoint-1" playOrder="1">
        <navLabel>
           <text>Cover</text>
        </navLabel>
      <content src="Morning Insight 26 April - K S 2/Morning Insight 26 April - K S 2/Morning Insight 26 April - K S/Morning Insight 26 April - K S/Morning Insight/OEBPS/first.html"/>
   </navPoint>

     <navPoint class="chapter" id="navpoint-2" playOrder="2">
        <navLabel>
          <text>News</text>
        </navLabel>
      <content src="Morning Insight 26 April - K S 2/Morning Insight 26 April - K S 2/Morning Insight 26 April - K S/Morning Insight 26 April - K S/Morning Insight/OEBPS/1.html"/>
   </navPoint>

    <navPoint class="chapter" id="navpoint-3" playOrder="3">
       <navLabel>
          <text>Report</text>
       </navLabel>
      <content src="Morning Insight 26 April - K S 2/Morning Insight 26 April - K S 2/Morning Insight 26 April - K S/Morning Insight 26 April - K S/Morning Insight/OEBPS/2.html"/>
    </navPoint>

    <navPoint class="chapter" id="navpoint-4" playOrder="4">
       <navLabel>
          <text>Economy Update</text>
       </navLabel>
      <content src="Morning Insight 26 April - K S 2/Morning Insight 26 April - K S 2/Morning Insight 26 April - K S/Morning Insight 26 April - K S/Morning Insight/OEBPS/3.html"/>
    </navPoint>

<navMap>

最后我的数组应该有:

array at 0: Cover
array at 1: News
array at 2: Report
array at 3: Economy Update

编辑:

- (NSString *)dataFilePath:(BOOL)forSave {
    NSLog(@"Path of my file=%@",TOC);
    return TOC;
}

-(void)viewDidAppear:(BOOL)animated{

    [super viewDidLoad];
    TOCArray=[[NSMutableArray alloc]init];

    NSString *filePath = [self dataFilePath:FALSE];
    NSData *response = [[NSMutableData alloc] initWithContentsOfFile:filePath];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:response options:0 error:nil];

    NSArray *navPoints = [[[doc.rootElement elementsForName:@"navMap"] lastObject] elementsForName:@"navPoint"];

    for (GDataXMLElement *m in navPoints)
    {
        NSArray *navLabel = [m elementsForName:@"navLabel"];
        for (GDataXMLElement *e in navLabel)
        {
            NSArray *text = [e elementsForName:@"text"];
            [TOCArray addObject:[text objectAtIndex:0]];
        }
    }

    for (int i=0; i<[TOCArray count]; i++) {
        NSLog(@"Toc array=%@",[TOCArray objectAtIndex:i]);
    }

}

我的日志显示:Path of my file=/Users/krunal/Library/Application Support/iPhone Simulator/5.0/Applications/D414BC19-C005-4D93-896D-A6FB71DE4D21/Documents/UnzippedEpub/toc.ncx

但我的数组仍然为空。

这是我的toc.ncx文件http://www.mediafire.com/?4r883s80he23cua

新编辑

2012-07-23 16:07:50.522  iBook [2105:fe03] Toc array=GDataXMLElement 0x6a96480: {type:1 name:text xml:"<text>Cover</text>"}
2012-07-23 16:07:50.522  iBook [2105:fe03] Toc array=GDataXMLElement 0x6a96610: {type:1 name:text xml:"<text>News</text>"}
2012-07-23 16:07:50.523  iBook [2105:fe03] Toc array=GDataXMLElement 0x6a96730: {type:1 name:text xml:"<text>Report</text>"}
2012-07-23 16:07:50.523  iBook [2105:fe03] Toc array=GDataXMLElement 0x6a96850: {type:1 name:text xml:"<text>Economy Update</text>"}
2012-07-23 16:07:50.524  iBook [2105:fe03] Toc array=GDataXMLElement 0x6a96970: {type:1 name:text xml:"<text>Other Factors</text>"}
2012-07-23 16:07:50.524  iBook [2105:fe03] Toc array=GDataXMLElement 0x6a96aa0: {type:1 name:text xml:"<text>Result Update</text>"}
2012-07-23 16:07:50.525  iBook [2105:fe03] Toc array=GDataXMLElement 0x6a96bc0: {type:1 name:text xml:"<text>Result Update (Continued)</text>"}
2012-07-23 16:07:50.526  iBook [2105:fe03] Toc array=GDataXMLElement 0x6a96ce0: {type:1 name:text xml:"<text>Trends in NIM</text>"}
2012-07-23 16:07:50.526  iBook [2105:fe03] Toc array=GDataXMLElement 0x6a96e00: {type:1 name:text xml:"<text>Key Stats</text>"}
2012-07-23 16:07:50.527  iBook [2105:fe03] Toc array=GDataXMLElement 0x6a96f40: {type:1 name:text xml:"<text>Result Update</text>"}
2012-07-23 16:07:50.527  iBook [2105:fe03] Toc array=GDataXMLElement 0x6a97060: {type:1 name:text xml:"<text>Disclaimer</text>"}
4

1 回答 1

3

GData我使用& TouchXML..为应用程序做了一些 xml 解析

创建一个数组来保存值。

假设您的服务调用返回NSData存储在响应中。从中创建一个GDataXMLDocument对象。

- (NSArray*)textArrayFromNCX
{
    NSMutableArray *values = [NSMutableArray array];

    // get the toc.ncx from documents folder
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    docPath = [docPath stringByAppendingPathComponent:@"UnzippedEpub"];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"toc" ofType:@"ncx" inDirectory:docPath];

    // init the xml document
    NSData *response = [[NSMutableData alloc] initWithContentsOfFile:filePath];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:response options:0 error:nil];

    // loop through the elements
    NSArray *navPoints = [[[doc.rootElement elementsForName:@"navMap"] lastObject] elementsForName:@"navPoint"];
    for (GDataXMLElement *m in navPoints)
    {
        NSArray *navLabel = [m elementsForName:@"navLabel"];
        for (GDataXMLElement *e in navLabel)
        {
            NSArray *text = [e elementsForName:@"text"];
            [values addObject:[[text lastObject] stringValue]];
        }
    }

    return values;
}

GData即使实际 xml 中只有一个标签/元素,库也会返回数组。内循环每次外循环迭代只运行一次。

要了解如何集成图书馆访问

http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml

于 2012-07-23T05:11:08.007 回答