iPhone:解析具有阿拉伯语内容的 Xml 显示空值
我正在尝试解析包含阿拉伯语单词的 XML 文件,如下所示:
<NewsComponent>
<HeadLine>العلاجية</HeadLine>
</NewsComponent>
当我 NSLog NSXMLParser 委托上的字符串时,它会打印空字符串,甚至当我将数据解析到 UITableView 时code> 它显示空文本。
在将数据传递给解析器之前,我将数据编码为 UTF-8。
如何在不丢失标签 HeadLine 内容的情况下解析 XMl?
注释:
1。与英语语言相同的 XML 可以正常工作。
2。在任何浏览器中显示 XML 都会正确显示数据。
3.在解析 NSString 之前将 NSData 转换为 NSString 和 NSLog-ing 也可以正确显示 xml。
Edit How am I doing this?
NSString *sURLREST = @"http://www.example.com/getXml";
NSURL *url = [NSURL URLWithString:sURLREST];
NSMutableURLRequest *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;
}
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];
NSMutableURLRequest *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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的错误在于
parser:foundCharacters:
。您不能期望每个元素仅调用该方法一次。它可能会被调用多次。您必须在第一次为每个元素调用该方法之前创建一个空的可变字符串(在parser:didStartElement:
中),然后将新字符附加到该字符串。在调用parser:didEndElement:
之前,该字符串并不完整。请参阅 Apple 的示例代码。他们做得正确。
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 (inparser:didStartElement:
) and then append the new characters to that string. The string is not complete untilparser:didEndElement:
is called.See Apple's sample code. They do it correctly.