在 iPhone 中读取 XML
我有一个如下所示的 XML 文件:
<Statistics>
<City ID="01">
<Town ID="04">
<Person ID="03" Name="Jack">
</Person>
<Person ID="04" Name="John">
</Person>
<Person ID="05" Name="Peter">
</Person>
<Person ID="06" Name="Daniel">
</Person>
</Town>
<Town>
...
</Town>
</City>
<City>
...
</City>
</Statistics>
我想读取此 XML 文件并创建一个城市数组。在城市数组内部,我想要具有给定 ID 的城镇数组,在城镇数组内部,我想要一个人员列表,我将在项目中使用他们的名字。 该系统将类似于:返回居住在 ID =“..”的城市和 ID =“...”的城镇的人 例如:返回居住在 CityID="01" 和 TownID="04" 的人员 该列表将是[杰克、约翰、彼得、丹尼尔]。
iPhone SDK 有办法做到这一点吗?如果有,您能否建议我一种执行此操作的方法或解释如何以最简单的方式执行此操作,因为我对这个环境非常陌生。
非常感谢您的答复。
I have a XML File like below:
<Statistics>
<City ID="01">
<Town ID="04">
<Person ID="03" Name="Jack">
</Person>
<Person ID="04" Name="John">
</Person>
<Person ID="05" Name="Peter">
</Person>
<Person ID="06" Name="Daniel">
</Person>
</Town>
<Town>
...
</Town>
</City>
<City>
...
</City>
</Statistics>
I want to read this XML file and create an array of cities. Inside city arrays I want to have Town arrays with given IDs and inside town arrays i want to have a person list that i am going to use their names in my project.
The system will be like: Return people that lives in city that has ID=".." and town that has ID="..."
For example: Return people that lives in CityID="01" and TownID="04"
The list will be [Jack, John, Peter, Daniel].
Is there way of doing this in iPhone SDK? If there is, can you suggest me a way of doing this or explain how to do this in a simplest way, because i am very new to this environment.
Thank you so much for the incoming answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,最好的解决方案是使用您选择的 xml 解析器,
如果您是 iphone sdk 和 xcode 等新手,为什么不使用 php 和简单的 xml 或 xml2array 呢?
你需要那个做什么?一个应用程序?
well the best solution is to use an xml parser of your choice
if your are new to iphone sdk and xcode etc why not working with php and simple xml or xml2array?
for what do you need that? an app?
这是一个示例...
// RootView.h
// AppDelegate.m
// RootView.m
Here is one example...
// RootView.h
// AppDelegate.m
// RootView.m
如果您可以控制
XML
(您是所有者),为什么不使用 plist(它使用XML
顺便说一句),然后使用[ 将其作为数组/字典获取NSArray arrayWithContentsOfFile:]
然后根据需要操作 NS 结构。If you can control the
XML
(you are the owner) why not use a plist(which usesXML
btw) and then get it as arrays/dicts with[NSArray arrayWithContentsOfFile:]
and then manipulate the NS structures however you want.iOS SDK 有多个 XML 解析选项可供选择。这是一篇关于这些选项的好读物:
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
你的问题听起来就像您想使用 XPath 一样。如果是这种情况,那么 TBXML 或 TouchXML 可能是最佳选择。
There s several XML parsing options to choose from for the iOS SDK. Here is a good read to here is a good read on those options:
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
Your question sounds like you would like to use XPath. If that is the case then TBXML or TouchXML may be the way to go.
NSXMLParser 是您正在寻找的:NSXMLParser 类参考
这是一个很好的教程:让 NSXMLParser 成为你的朋友
Sample Code:
您将需要此委托:
并使用
NSDictionary
类型的attribute
变量来读取标签属性:不要忘记添加
NSXMLParserDelegate
到你的类的协议:NSXMLParser is what you are looking for: NSXMLParser Class Reference
This is a Good Tutorial: Make NSXMLParser your friend
Sample Code:
You are going to need this delegate:
And use the
attribute
variable of typeNSDictionary
to be able to read the tag attributes:Don't forget to add the
NSXMLParserDelegate
protocol to your class: