iPhone:解析具有阿拉伯语内容的 Xml 显示空值

发布于 2024-12-01 11:40:15 字数 1684 浏览 0 评论 0原文

我正在尝试解析包含阿拉伯语单词的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别忘他 2024-12-08 11:40:15

您的错误在于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 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文