I'm developing a project which fetch data from a php file(as XML).To be more clearly the app works like this:
1-Send an HTTP Request to a PHP file,which select data from a mysql database and display it as XML,using the code bellow:
<?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>
This part is working fine,every type of chars are displayed well in my web browser.Now the second step is to get those data to my iOS application,for this i'm using the code bellow:
-(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;}
}
Again everything works well,but the problem is,if one of the string from the XML file contain any special chars(such as: ´,ˆ,˜,-),the app stays inert.I can only suppose that the problem is about strings encoding!Someone knows how to fix this problem?
Note:The value fetched from the XML file is stored on the msgError(NSMutableString). Thanks in advance!