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)