Cocoa Touch / iPhone中的XML解析 [英] XML Parsing in Cocoa Touch/iPhone

查看:106
本文介绍了Cocoa Touch / iPhone中的XML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我看到过TouchXML,parseXML,NSXMLDocument,NSXMLParser,但我真的很困惑要做什么。

Okay i have seen TouchXML, parseXML, NSXMLDocument, NSXMLParser but i am really confused with what to to do.

我有一个iphone应用程序连接到服务器,请求数据并获取XML响应。通过 http://pastebin.com/f681c4b04

I have an iphone app which connects to a servers, requests data and gets XML response. Sample xml response to different set of queries is give at http://pastebin.com/f681c4b04

我有另一个类作为Controller(如在MVC中,做数据的读取逻辑)。这个类从View类获取输入,并处理它。向web服务器发送请求,获取xml,解析xml,填充其变量(其单个/共享的类),然后响应为true或false到调用者。调用者基于控制器类给出的响应,检查控制器的变量并向用户显示适当的内容。

I have another classes which acts as Controller (As in MVC, to do the logic of fetch the data). This class gets the input from the View classes and processes it e.g. send a request to the webserver, gets xml, parses xml, populates its variables (its a singleton/shared Classes), and then responses as true or false to the caller. Caller, based on response given by the controller class, checks controller's variables and shows appropriate contents to the user.

我有以下控制器类变量:

I have the following Controller Class variables:

@interface backendController : NSObject {
NSMutableDictionary *searchResults, *plantInfoResults, *bookmarkList, *userLoginResult;
}

,函数类似getBookmarkList,getPlantInfo。现在我打印由web服务器的纯XML返回
NSLog(@Result::%@[NSString stringWithContentsOfURL:url])

and functions like getBookmarkList, getPlantInfo. Right now i am printing plain XML return by the webserver by NSLog(@"Result: :%@" [NSString stringWithContentsOfURL:url])

一个通用函数,它获取从服务器返回的XML,对其进行解析,将包含XML开始标签的文本表示形式的NSMutableDictionary作为键和XML标签值作为值,并返回。

I want a generic function which gets the XML returned from the server, parseses it, makes a NSMutableDictionary of it containing XML opening tags' text representation as Keys and XML Tag Values as Values and return that.

只有一个问题,怎么办?

Only one question, how to do that?.

推荐答案

您是否尝试过任何XML解析器?我认为关于NSXMLParser的教程将有所帮助。这是他们如何设置节点名的键值:

Have you tried any of the XML Parsers you mentioned? I think this tutorial on NSXMLParser will help. This is how they set the key value of a node name:

[aBook setValue:currentElementValue forKey:elementName];

仔细检查你的XML虽然,似乎你缺少一个根节点的一些结果。除非你为了简单而离开它。

P.S. Double check your XML though, seems you are missing a root node on some of your results. Unless you left it out for simplicity.

查看 w3schools XML教程,它应该指向正确的XML语法方向。

Take a look at w3schools XML tutorial, it should point you in the right direction for XML syntax.

这篇关于Cocoa Touch / iPhone中的XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆