【问题标题】:Parse XML file from web site into UITextView将网站中的 XML 文件解析为 UITextView
【发布时间】:2014-02-08 06:10:18
【问题描述】:

我正在开发适用于 iOS 的每日圣经引用应用程序,我想知道如何解析在线托管的 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 中解析&lt;title&gt;,在另一个UITextView 中解析&lt;text&gt;。有人能帮忙吗?使用 Apple 的 NSXMLParser 是否可行?

【问题讨论】:

标签: ios xml xcode


【解决方案1】:

如果结果数始终为 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;

【讨论】:

  • 详细了解如何将 XML 文件中的两个指定元素解析为 UITextView,以及如何处理..
【解决方案2】:

我用来解析一个xml通常是这个lib

https://github.com/nicklockwood/XMLDictionary

解析xml并使用数据填充tableView非常容易

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 2021-06-11
    • 2012-02-12
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    相关资源
    最近更新 更多