我正在为 IOS 5+ 开发带有 xCode 4.5 的 iPhone 应用程序,我正在使用 ARC 和 Storyboard
我目前正在将一个 XML 项目列表绑定到一个NSObject
.
目前,我可以成功提取每个节点的内容,但我需要读取一个节点的属性,并将多个同名节点存储在一个数组中。
我想从这样的 XML 中提取所有信息(值 1、2、3,分别绑定到属性p1
、、、p2
)p3
:
<Contact>
<nom>Amanda</nom>
<prenom>Pattison</prenom>
<tel>3211234567</tel>
<email>joe@smith.com</email>
<array>
<property name="p1" value="1"/>
<property name="p2" value="2"/>
<property name="p3" value="3"/>
</array>
</Contact>
我目前正在使用这个 XML(本地托管):
<?xml version="1.0" encoding="UTF-8"?>
<contacts>
<Contact>
<nom>Amanda</nom>
<prenom>Pattison</prenom>
<tel>3211234567</tel>
<email>joe@smith.com</email>
</Contact>
<Contact>
<nom>Kraft</nom>
<prenom>Philippe</prenom>
<tel>0728812121</tel>
<email/>
</Contact>
<Contact>
<nom>Kent</nom>
<prenom>Jerome</prenom>
<tel>087832832</tel>
<email>contact@jkraft.fr</email>
</Contact>
<Contact>
<nom>Melbourne</nom>
<prenom>Yannis</prenom>
<tel>321-123-4567</tel>
<email>joe@smith.com</email>
</Contact>
<Contact>
<nom>Canberra</nom>
<prenom>Amstrong</prenom>
<tel>0728812121</tel>
<email/>
</Contact>
<Contact>
<nom>Namali</nom>
<prenom>Pat</prenom>
<tel>087832832</tel>
<email>contact@jkraft.fr</email>
</Contact>
<Contact>
<nom>Malani</nom>
<prenom>John</prenom>
<tel>321-123-4567</tel>
<email>joe@smith.com</email>
</Contact>
<Contact>
<nom>Pompidou</nom>
<prenom>Juan</prenom>
<tel>0728812121</tel>
<email/>
</Contact>
<Contact>
<nom>Cauht</nom>
<prenom>Frederic</prenom>
<tel>087832832</tel>
<email>contact@jkraft.fr</email>
</Contact>
<Contact>
<nom>Neil</nom>
<prenom>Joe</prenom>
<tel>321-123-4567</tel>
<email>joe@smith.com</email>
</Contact>
<Contact>
<nom>Youn</nom>
<prenom>Eric</prenom>
<tel>0728812121</tel>
<email/>
</Contact>
<Contact>
<nom>Lee</nom>
<prenom>Pascal</prenom>
<tel>087832832</tel>
<email>contact@jkraft.fr</email>
</Contact>
</contacts>
我正在使用这个解析器:
-#XMLToObjectParser.h
#import <Foundation/Foundation.h>
@interface XMLToObjectParser : NSObject <NSXMLParserDelegate> {
NSString *className;
NSMutableArray *items;
NSObject *item;
NSString *currentNodeName;
NSMutableString *currentNodeContent;
}
- (NSArray *)items;
- (id)parseXMLAtURL:(NSURL *)url
toObject:(NSString *)aClassName
parseError:(NSError **)error;
@end
-#XMLToObjectParser.m
#import "XMLToObjectParser.h"
@implementation XMLToObjectParser
- (NSArray *)items
{
return items;
}
- (id)parseXMLAtURL:(NSURL *)url
toObject:(NSString *)aClassName
parseError:(NSError **)error
{
items = [[NSMutableArray alloc] init];
className = aClassName;
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
if([parser parserError] && error) {
*error = [parser parserError];
}
return self;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
NSLog(@"Open tag: %@", elementName);
if([elementName isEqualToString:className]) {
// create an instance of a class on run-time
item = [[NSClassFromString(className) alloc] init];
}
else {
currentNodeName = [elementName copy];
currentNodeContent = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
NSLog(@"Close tag: %@", elementName);
if([elementName isEqualToString:className]) {
[items addObject:item];
item = nil;
}
else if([elementName isEqualToString:currentNodeName]) {
// use key-value coding
[item setValue:currentNodeContent forKey:elementName];
currentNodeContent = nil;
currentNodeName = nil;
}
}
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{
NSLog(@"Found tag: %@", string);
[currentNodeContent appendString:string];
}
@end
来电:
tableau = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString: @"http://10.20.100.12:8888/contacts.xml"];
XMLToObjectParser *myParser = [[XMLToObjectParser alloc] parseXMLAtURL:url toObject:@"Contact" parseError:nil];
for(int i = 0; i < [[myParser items] count]; i++) {
Contact *new = [[Contact alloc] init];
new = (Contact *)[[myParser items] objectAtIndex:i];
[tableau addObject:new];
//[new release];
}