我一直在尝试用 php 脚本的 xml 输出填充 Xcode 中的数组,以便 ios 可以连接到 mysql。但是,尽管 mysql 中有信息,但我的数组仍然是一个空值。这一定意味着我的属性部分或我对 xml 的解析是错误的,但我真的不知道实际上是什么错误(并且没有多少星标似乎有帮助)。我没有从 php 或 xcode 中得到任何错误。有任何想法吗?
编辑:我遵循了以下建议,但我仍然得到一个空值,尽管我相信它来自我的 php ......
- (void)viewDidLoad
{
[super viewDidLoad];
char *cStr = "YES";
NSString *str3 = [NSString stringWithUTF8String:cStr];
NSString *urlString = [NSString stringWithFormat:@"(censored)", _login];
NSXMLParser *Parser = [[[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
[Parser setDelegate:self];
[Parser parse];
NSDictionary *itemAtIndex =(NSDictionary *)[oneam objectAtIndex:0];
oneam = [[NSMutableArray alloc] init];
if (([[itemAtIndex objectForKey:@"its"]isEqualToString:str3 ])) {
[switch1 setOn:YES animated:YES];
}
else {
[switch1 setOn:NO animated:YES];
}
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ( [elementName isEqualToString:@"location"]) {
[oneam addObject:[[NSDictionary alloc] initWithDictionary:attributeDict]];
NSLog(@"The array is %@", oneam);
}
}
这是我的php ...
<?php
$login = "hello";
$dbh = new PDO('(censored)');
$sql = "SELECT oneam FROM login WHERE username = '$login'";
$q = $dbh->prepare( $sql );
$doc = new DOMDocument();
$r = $doc->createElement( "oneam" );
$doc->appendChild( $r );
foreach ( $q->fetchAll() as $row) {
$e = $doc->createElement( "location" );
$e->setAttribute( 'its', $row['oneam']);
$r->appendChild( $e );
}
print $doc->saveXML();
?>