从iPhone中的html页面获取标记值

时间:2012-07-21 16:42:30

标签: iphone ipad delegates nsmutablearray nsxmlparser

我是iPhone新手,

我想从我的本地<text>页面获取.html标记的所有值,我想将其存储在我的数组中。

例如:toc.html是我的html文件,我希望从此文件中获取<text>标记的所有值。

这是我的html文件,

<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

任何帮助都会得到满足。

1 个答案:

答案 0 :(得分:1)

尝试,

- (NSString *)dataFilePath:(BOOL)forSave {
        //TOC is a path of your `html` file
        return TOC;
    }

存储<text>内部数组

的内容
-(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]];
        }
    }

}