Web Url包含我的NSXMLParser未解析的西班牙语字符

时间:2011-11-14 10:16:56

标签: iphone objective-c xcode ios4 nsxmlparser

我从服务器解析Web URL并将它们存储在Strings中,然后在Webview上显示它。但是现在当我正在解析像

这样的西班牙语单词时
http://litofinter.es.milfoil.arvixe.com/litofinter/PDF/Presentación_LITOFINTER_(ES).pdf

它正在接受它

PDF File Name  ++++++ http://litofinter.es.milfoil.arvixe.com/litofinter/PDF/Presentaci
PDF File Name  ++++++ ón_LITOFINTER_(ES).pdf

即两个不同的字符串...我知道我必须做一些小的改动,即附加字符串,但我现在无法做到,任何人都可以帮助我。这是我的代码: -

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    currentElement = elementName;

    if([currentElement isEqualToString:@"category"]) {

        NSLog(@"Current element in Category:- %@",currentElement);
        obj = [[Litofinter alloc]init];
        obj.productsArray = [[NSMutableArray alloc]init];
    }

    if([currentElement isEqualToString:@"Product"]) {
        obj.oneObj = [[Products alloc]init];
    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if([currentElement isEqualToString:@"Logo"]) {
        obj.cLogo=[NSString stringWithFormat:@"%@",string];
        NSLog(@"Logo to be saved in Array :- %@",obj.cLogo);
    }

    if([currentElement isEqualToString:@"Name"]) {

        obj.cName=[NSString stringWithFormat:@"%@",string];
        NSLog(@"Name to be saved in Array :- %@",string);
    }

    if([currentElement isEqualToString:@"cid"]) {

        obj.cId=(int)[NSString stringWithFormat:@"%@",string];
        NSLog(@"CID to be saved in Array :- %@",string);
    }

    if([currentElement isEqualToString:@"pid"]) {
        //obj.oneObj.id = (int)[NSString stringWithFormat:@"%@",oneBook.id];
        obj.oneObj.id = (int)[oneBook.id intValue];
    }

    if([currentElement isEqualToString:@"Title"]) {
        obj.oneObj.title = [NSString stringWithFormat:@"%@",string];
    }

    if([currentElement isEqualToString:@"Thumbnail"]) {
        obj.oneObj.thumbnail= [NSString stringWithFormat:@"%@",string];
    }

    // problem occuriing while parsing Spanish characters...

    if([currentElement isEqualToString:@"pdf"]) {
        obj.oneObj.pdf = [NSString stringWithFormat:@"%@",string];
        NSLog(@"PDF File Name  ++++++ %@",string);
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"category"]) {
        NSLog(@"Current element in End Element Category:- %@",currentElement);

        [TableMutableArray addObject:obj];

    }

    if([elementName isEqualToString:@"Product"]) {

        [obj.productsArray addObject:obj.oneObj];
    }

    currentElement = @"";
}

我会感激你的。

2 个答案:

答案 0 :(得分:3)

我找到了答案:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    currentElement = elementName;


    // MOdified

    else if([currentElement isEqualToString:@"pdf"]) {
    //  obj.oneObj.pdf = [NSString stringWithFormat:@"%@",string];
    //  NSLog(@"PDF File Name  ++++++ %@",string);

        NSMutableString *outputBuilder =  [[NSMutableString alloc]init] ;

        [outputBuilder appendString:[NSString stringWithFormat:@"%@", self.pdfString]];

        [outputBuilder appendString:[NSString stringWithFormat:@"%@", string]];

        self.pdfString = outputBuilder;

        [outputBuilder release];
    }

    else 
    {
        self.pdfString = string;
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

    if([currentElement isEqualToString:@"pdf"])
    {
        obj.oneObj.pdf = self.pdfString;
        NSLog(@"PDF File Name  ++++++ %@",obj.oneObj.pdf);
    }

    currentElement = @"";
}

还有一个很小的变化,也就是我传递这个的地方: -

NSString *urlString = [appDelegate.currentBookPressed stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

答案 1 :(得分:1)

declare NSString in .h file;

in Did Start parse

str=@"";

if([currentElement isEqualToString:@"pdf"]) {
    str=[str stringByAppendingString:string];
    obj.oneObj.pdf = [NSString stringWithFormat:@"%@",str];
    NSLog(@"PDF File Name  ++++++ %@",str);
}