I am trying to parse XML file which contains arabic words as follow:
<NewsComponent>
<HeadLine>العلاجية</HeadLine>
</NewsComponent>
when I NSLog the string on the NSXMLParser
delegate it prints empty string, and even when i parse the data to the UITableView
it shows empty text.
I am encoding the data as UTF-8 before passing it to the parser.
How can I parse the XMl without losing the content of the tag HeadLine?
notes:
1. The same XML with English language is working correctly.
2. Showing the XML in Any browser shows the data correctly.
3. converting the NSData to NSString and NSLog-ing before parsing the NSString show the xml correctly too.
Edit How am I doing this?
NSString *sURLREST = @"http://www.example.com/getXml";
NSURL *url = [NSURL URLWithString:sURLREST];
NSMutabl开发者_JS百科eURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLResponse *response;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
NSString* output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData* dataOutput = [output dataUsingEncoding: NSUTF8StringEncoding];
NSXMLParser *parser = [[NSXMLParser alloc] dataOutput];
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if([sCurrentItem isEqualToString:@"HeadLine"])
{
[mutuableArray addObject:string];
// I am just adding the string value to a NSMutuableArray to bind it with the UITableView.
NSLog(@"%@",string);
}
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
sCurrentItem = elementName;
}
Your error lies in parser:foundCharacters:
. You cannot expect that that method will be called only once per element. It may be called multiple times. You have to create an empty mutable string before the method is called the first time per element (in parser:didStartElement:
) and then append the new characters to that string. The string is not complete until parser:didEndElement:
is called.
See Apple's sample code. They do it correctly.
精彩评论