2014
Mar
22

要 Parse xml 之前,要先下載別人寫好的 XMLParser Library,這裡我使用的是「xml parse」,使用方式還算簡單,這裡我先定義一個 XML String ,然後用 dictionaryForXMLString 將 XML 讀進來,範例如下。

xml parse
  1. #import "xmlReader.h";
  2.  
  3. NSString *xmlContent = [NSString stringWithFormat:@"<?xml version="1.0" encoding="UTF-8" ?>"
  4. "<xml>"
  5. "<title>IOS Tutorial</title>"
  6. "<desc>IOS desc</desc>"
  7. "</xml>"];
  8. NSDictionary *dics=[[NSDictionary alloc]
  9. initWithDictionary:[XMLReader dictionaryForXMLString:xmlContent error:nil]
  10. ];
  11. NSLog(@"dics is %@",dics);

將 dics 物件輸出的結果如下,可以清楚的看到其資料結構。

result
  1. dics is {
  2. xml = {
  3. desc = {
  4. text = "IOS desc";
  5. };
  6. title = {
  7. text = "IOS Tutorial";
  8. };
  9. };
  10. }

上述的範例,只是直接將 NSDictionary 輸出至螢幕,接下來我要取得 titile 的這個 tag 的值。

Example
  1. NSArray *tableDicsArray=[dics valueForKey:@"xml"];
  2. NSLog(@"data = %@", [[tableDicsArray valueForKey:@"title"] valueForKey:@"text"]);
  3. //output: IOS Tutorial

接著來試一下 Array 的處理,假如我的 XML 格式包含多項的 List,我們一樣可以用 Array[0], Array[1] 的方式取得資料。

XML Array
  1. NSString *xmlContent = [NSString stringWithFormat:@"<?xml version="1.0" encoding="UTF-8" ?>"
  2. "<xml>"
  3. "<item><title>IOS Tutorial</title>"
  4. "<desc>IOS desc</desc></item>"
  5. "<item><title>IOS Tutorial2</title>"
  6. "<desc>IOS desc2</desc></item>"
  7. "</xml>"];
  8. NSDictionary *dics=[[NSDictionary alloc]initWithDictionary:[XMLReader dictionaryForXMLString:xmlContent error:nil]];
  9. NSArray *tableDicsArray=[[dics valueForKey:@"xml"] valueForKey:@"item"];
  10. NSLog(@"data = %@", [[tableDicsArray[1] valueForKey:@"title"] valueForKey:@"text"]);

RSS Content Parse

這是一個簡單的 RSS 格式,與 Parse 範例。

Example
  1. NSString *xmlContent = [NSString stringWithFormat:@"<?xml version="1.0" encoding="UTF-8" ?>"
  2. "<rss version="2.0">"
  3. "<title>Puritys Program Blog</title>"
  4. "<channel>"
  5. "<title>Puritys Program Blog</title>"
  6. "<link>http://www.puritys.me/rss/blog</link>"
  7. "<description></description>"
  8. "<language>zh-tw</language>"
  9. "<pubDate>Sat, 22 Mar 2014 23:10:03 +0800</pubDate>"
  10. "<copyright>www.ewebsite.com.tw</copyright>"
  11. "<generator>Puritys</generator>"
  12. "<item>"
  13. "<title>Javascript Hoisting 與 Closure</title>"
  14. "<link>http://www.puritys.me/docs-blog/article-242</link>"
  15. "<description>content</description>"
  16. "<pubDate>Sat, 22 Mar 2014 00:23:18 +0800</pubDate>"
  17. "<guid></guid>"
  18. "</item>"
  19. "<item>"
  20. "<title>PHP 判斷是否為 Command Line 的方式</title>"
  21. "<link>http://www.puritys.me/docs-blog/article-241</link>"
  22. "<description>content</description>"
  23. "<pubDate>Sat, 22 Mar 2014 00:23:18 +0800</pubDate>"
  24. "<guid></guid>"
  25. "</item>"
  26. "</channel>"
  27. "</rss>"];
  28. NSDictionary *dics=[[NSDictionary alloc]initWithDictionary:[XMLReader dictionaryForXMLString:xmlContent error:nil]];
  29. NSArray *tableDicsArray=[[[dics valueForKey:@"rss"] valueForKey:@"channel"] valueForKey:@"item"];

相關檔案下載


回應 (Leave a comment)