在 iPhone 中读取 XML

发布于 2024-11-07 05:39:56 字数 913 浏览 1 评论 0原文

我有一个如下所示的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心病无药医 2024-11-14 05:39:57

好吧,最好的解决方案是使用您选择的 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?

那支青花 2024-11-14 05:39:57

这是一个示例...

// RootView.h

@interface RootView : UIViewController <NSXMLParserDelegate> {
    NSXMLParser *xmlParser;
}

// AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    [rootView parseXmlFile:[[NSBundle mainBundle] pathForResource:@"Untitled" ofType:@"xml"]];
}

// RootView.m

- (void)parseXmlFile:(NSString *)pathToFile {
    BOOL success;
    NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];
    if (xmlParser) // xmlParser is an NSXMLParser instance variable
        [xmlParser release];
    xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [xmlParser setDelegate:self];
    success = [xmlParser parse]; // return value not used
    // if not successful, delegate is informed of error
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {

    if ([elementName isEqualToString:@"City"]) {
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"City"]) {
    }
}

Here is one example...

// RootView.h

@interface RootView : UIViewController <NSXMLParserDelegate> {
    NSXMLParser *xmlParser;
}

// AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    [rootView parseXmlFile:[[NSBundle mainBundle] pathForResource:@"Untitled" ofType:@"xml"]];
}

// RootView.m

- (void)parseXmlFile:(NSString *)pathToFile {
    BOOL success;
    NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];
    if (xmlParser) // xmlParser is an NSXMLParser instance variable
        [xmlParser release];
    xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [xmlParser setDelegate:self];
    success = [xmlParser parse]; // return value not used
    // if not successful, delegate is informed of error
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {

    if ([elementName isEqualToString:@"City"]) {
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"City"]) {
    }
}
何以心动 2024-11-14 05:39:57

如果您可以控制 XML(您是所有者),为什么不使用 plist(它使用 XML 顺便说一句),然后使用 [ 将其作为数组/字典获取NSArray arrayWithContentsOfFile:] 然后根据需要操作 NS 结构。

If you can control the XML(you are the owner) why not use a plist(which uses XML btw) and then get it as arrays/dicts with [NSArray arrayWithContentsOfFile:] and then manipulate the NS structures however you want.

妥活 2024-11-14 05:39:57

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.

喜爱皱眉﹌ 2024-11-14 05:39:56

NSXMLParser 是您正在寻找的:NSXMLParser 类参考

这是一个很好的教程:让 NSXMLParser 成为你的朋友


Sample Code:

 NSString* XMLString = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>...";

 NSData* data = [XMLString dataUsingEncoding:NSUTF8StringEncoding]; 

 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]

 [parser setDelegate:self]; 
 [parser setShouldProcessNamespaces:NO]; 
 [parser setShouldReportNamespacePrefixes:NO];
 [parser setShouldResolveExternalEntities:NO]; 

 [parser parse]; 

您将需要此委托:

 parser:didStartElement:namespaceURI:qualifiedName:attributes:

并使用 NSDictionary 类型的 attribute 变量来读取标签属性:

 [[attributeDict objectForKey:@"id"] integerValue]
 [attributeDict objectForKey:@"Name"]

不要忘记添加 NSXMLParserDelegate 到你的类的协议:

#import <Foundation/Foundation.h>

@interface MyClass : NSObject <NSXMLParserDelegate> {
...

NSXMLParser is what you are looking for: NSXMLParser Class Reference

This is a Good Tutorial: Make NSXMLParser your friend


Sample Code:

 NSString* XMLString = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>...";

 NSData* data = [XMLString dataUsingEncoding:NSUTF8StringEncoding]; 

 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]

 [parser setDelegate:self]; 
 [parser setShouldProcessNamespaces:NO]; 
 [parser setShouldReportNamespacePrefixes:NO];
 [parser setShouldResolveExternalEntities:NO]; 

 [parser parse]; 

You are going to need this delegate:

 parser:didStartElement:namespaceURI:qualifiedName:attributes:

And use the attribute variable of type NSDictionary to be able to read the tag attributes:

 [[attributeDict objectForKey:@"id"] integerValue]
 [attributeDict objectForKey:@"Name"]

Don't forget to add the NSXMLParserDelegate protocol to your class:

#import <Foundation/Foundation.h>

@interface MyClass : NSObject <NSXMLParserDelegate> {
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文