0
我正在開發從一個php文件讀取數據(XML)的一個項目。如要更清楚應用程序是這樣的:Iphone XML數據增強?
1 - 發送一個HTTP請求到PHP文件,它從選擇數據MySQL數據庫和顯示爲XML,使用代碼波紋管:
<?php
header("Content-Type: text/html; charset=ISO-8859-1",true);
echo(utf8_decode($xml->title));
$connect = mysql_connect("XX.XX.com", "XXX", "XXX") or die(mysql_error());
mysql_select_db("XXX") or die(mysql_error());
$id_get = $_GET['id'];
$selectData = "SELECT * FROM teste_xml_accentuation WHERE id = '$id_get' ";
$selectedData = mysql_query($selectData);
?>
<chat>
<?php while ($row = mysql_fetch_assoc($selectedData)) { $id = $row['id']; $added = $row['added']; $text = $row['text']; ?>
<teste added="<?php echo"$added"; ?>" id="<?php echo"$id"; ?>">
<id> <?php echo"$id"; ?> </id>
<added> <?php echo"$added"; ?> </added>
<text> <?php echo"$text"; ?> </text>
</teste>
<?php } ?>
</chat>
這部分工作正常,每一種類型的字符都在我的網頁顯示以及browser.Now第二步是這些數據讓我的的iOS應用程序,爲此,我使用代碼波紋管:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"teste"]) {
msgAdded = [[attributeDict objectForKey:@"added"] retain];
msgId = [[attributeDict objectForKey:@"id"] intValue];
msgIDEN = [[NSMutableString alloc] init];
msgError = [[NSMutableString alloc] init];
inIDEN = NO;
inError = NO;
}
if ([elementName isEqualToString:@"id"]) { inIDEN = YES; }
if ([elementName isEqualToString:@"text"]) { inError = YES; }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (inIDEN) { [msgIDEN appendString:string]; }
if (inError) { [msgError appendString:string]; }
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"teste"]) {
[messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:msgIDEN,@"id",msgAdded,@"added",msgError,@"text",msgErroNSS,@"text",nil]];
[[messages reverseObjectEnumerator] allObjects];
lastId = msgId;
[msgAdded release];
[msgErroNSS release];
[msgError release];
NSLog(msgErroNSS);
NSLog(msgError);
NSLog(msgAdded);
NSLog(@"Magalhães");
}
if ([elementName isEqualToString:@"id"] ) { inIDEN = NO;}
if ([elementName isEqualToString:@"text"] ) { inError = NO;}
}
再次一切正常,但問題是,如果XML文件中的一個字符串包含任何特殊字符(如:',,〜, - ),應用程序保持惰性。我只能假設問題是關於字符串編碼!有人知道如何解決這個問題?
注意:從XML文件中獲取的值存儲在msgError(NSMutableString)中。 在此先感謝!