从网站解析XML文件到UITextView
问题描述:
我正在为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
一个可行的选择?
答
如果结果数总是,那么您可以将字符串拆分以获取必需的字段。
例如,对于您的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
答
我用它来解析XML通常这LIB
https://github.com/nicklockwood/XMLDictionary
这是很容易解析xml并使用这些数据填充你的表格查看
NSDictionary * baseDic = [NSDictionary dictionaryWithXMLData:[NSData dataWithContentsOfFile:filePath]];
也许这可以帮助:http://*.com/a/9884929/1153630 –