0

My NSXMLParser breaks on this string:

<title>AAA &#8211; BCDEFGQWERTYUIO</title>

I parsed it in this way, hope is the right way:

- (void) parser: (NSXMLParser *) parser foundCharacters: (NSString *) string{
[...]
if ([currentElement isEqualToString:@"title"]) {
    if (![string isEqualToString:@""]) {
            [title addObject:string];
            NSLog(@"str: %@", string);
    }
}

it returns me:

str: AAA
str: -
str: BCDEFGQWERTYUIO

But i want to return a single string:

str: AAA - BCDEFGQWERTYUIO

because it's the correct title.

Any idea?

Thanks.

Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
elp
  • 8,021
  • 7
  • 61
  • 120

2 Answers2

1

I find that it is better to create your object in the didEndElement method. That way you can just keep appending the string data to some temporary string. So you might do something like below (NOTE: code not tested)

In your object header:

NSMutableString *currentElementData;

...

@property (nonatomic, retain) NSMutableString *currentElementData;

And in your parsing code you would have

- (void) parser: (NSXMLParser *) parser foundCharacters: (NSString *) string{
[...]
if ([currentElement isEqualToString:@"title"]) {
    [currentElementData appendString:string];
    }
}

Then later in your element did end

   - (void) parser: (NSXMLParser *) parser didEndElement: (NSString *) elementName...{
    [...]
    if ([elementName isEqualToString:@"title"]) {
        [title addObject:currentElementData];
         [currentElementData setString:@""];
        }
    }
Walter
  • 5,867
  • 2
  • 30
  • 43
0

you can use CDATA section in your xml because characters like "<" and "&" are illegal in XML elements.

"<" will generate an error because the parser interprets it as the start of a new element.

"&" will generate an error because the parser interprets it as the start of an character entity.

Gyani
  • 2,241
  • 1
  • 24
  • 38
  • I cannot use CDATA, because it is a parser from Wordpress Feed rss and i can't edit xml. It use CDATA only in "description" tag. – elp Dec 22 '10 at 11:58