2014-02-08 75 views
0

我正在爲iOS應用程序開發一天的聖經報價,並想知道如何解析XML在線託管的數據以在我的應用程序中顯示在UITextView中。從網站解析XML文件到UITextView

<bible> 
<title>John 1:14</title> 
<item> 
<bookname>John</bookname> 
<chapter>1</chapter> 
<verse>14</verse> 
<text> 
Now the Word became flesh and took up residence among us. We saw his glory – the glory of the one and only, full of grace and truth, who came from the Father. 
</text> 
</item> 
<results>1</results> 
</bible> 

我想在另一個UITextView一個UITextView<text>解析<title>的。有人能幫忙嗎?正在使用蘋果的NSXMLParser一個可行的選擇?

+0

也許這可以幫助:http://stackoverflow.com/a/9884929/1153630 –

回答

1

如果結果數總是,那麼您可以將字符串拆分以獲取必需的字段。

例如,對於您的XML字符串,

代碼

NSString *data = @"<bible><title>John 1:14</title><item><bookname>John</bookname><chapter>1</chapter> <verse>14</verse> <text> Now the Word became flesh and took up residence among us. We saw his glory – the glory of the one and only, full of grace and truth, who came from the Father. </text> </item> <results>1</results> </bible>"; 
NSString *title = [data componentsSeparatedByString:@"<title>"][1]; 
title=[title componentsSeparatedByString:@"</title>"][0]; 
NSLog(@"%@",title); 

NSString *text = [data componentsSeparatedByString:@"<text>"][1]; 
text=[text componentsSeparatedByString:@"</text>"][0]; 
NSLog(@"%@",text); 

// now assign them to textViews 
titleTextView.text=title; 
textTextView.text=text; 
+0

更多關於如何解析XML文件中的兩個指定元素到UITextView,以及如何做.. – iPwnTech

1

我用它來解析XML通常這LIB

https://github.com/nicklockwood/XMLDictionary

這是很容易解析xml並使用這些數據填充你的表格查看

NSDictionary * baseDic = [NSDictionary dictionaryWithXMLData:[NSData dataWithContentsOfFile:filePath]];