2012-07-21 20 views
0

我新的iPhone,從HTML頁面獲取標籤值在iPhone

我想從我的本地.html網頁抓取<text>標籤的所有價值,我想將它保存在我的數組。

例如: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 

任何幫助將appriciated。

回答

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]]; 
     } 
    } 

}