2012-07-20 98 views
1

我是新來的iPhone開發者,取從HTML頁面內容表中iPhone

我做了EPUB閱讀我的應用程序,其中我想從我的本地.html頁,我取<text>標籤的所有價值想要將其存儲在我的數組中。

對於如: 這是我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>"} 
+0

試圖解析HTML或XML解析集成? – Nina 2012-07-20 06:40:14

+0

NSXml解析.. – Krunal 2012-07-20 06:54:31

+0

多數民衆贊成在很.. ..你有什麼輸出! – Nina 2012-07-20 08:14:27

回答

3

我做了一些XML解析使用GData & TouchXML ..

創建一個數組來保存值的應用程序。

假設您的服務呼叫返回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

+0

Plz請參閱我的**編輯** – Krunal 2012-07-23 10:20:30

+0

您是否嘗試過調試以確定外循環是否正在運行? – tGilani 2012-07-23 10:30:56

+0

嘗試用這行代替'NSArray * navPoints = [[[doc.rootElement elementsForName:@「navMap」] lastObject] elementsForName:@「navPoint」];' – 2012-07-23 10:33:21