Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

iphone - NSXMLParser don't get all the tag if the tag have accent

I have a webservice with diferent changes for the DataBase. When I consume the webservice return lines with sql tags, like this:

<sql>
   DELETE FROM TABLE WHERE ...
</sql>
<sql>
   INSERT INTO TABLE WHERE ...
</sql>
<sql>
   UPDATE TABLE SET ... WHERE ...
</sql>

I save this codes in the NSMutableArray like this:

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName  {
    if ([elementName isEqualToString:@"sql"]){
        [maResultado addObject:[NSMutableString stringWithFormat:@"%@", ResultadoSoap]];
        [ResultadoSoap setString:@""];
    }
}

This is good, but if the webservice returns a word with an accent (accent because is in spanish), the app only get the phrase after the accent. Example: the web service return this:

<sql>
   INSERT INTO DATOS(datos, fecha) VALUES('Indagar si el médico a tenido oportunidad de ...', '20/02/2013')
</sql>

I do this:

if ([elementName isEqualToString:@"sql"]){
    [maResultado addObject:[NSMutableString stringWithFormat:@"%@", ResultadoSoap]];
    [ResultadoSoap setString:@""];
}

and in the maResultado in the objectatindex only have this:

édico a tenido oportunidad de ...', '20/02/2013')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The parser:foundCharacters: delegate function can be called more than once for an XML element. This happens in particular if the character content contains special characters. In your case, for the data

<sql>
   INSERT INTO DATOS(datos, fecha) VALUES('Indagar si el médico a tenido oportunidad de ...', '20/02/2013')
</sql>

the delegate function is called twice, first with the string

INSERT INTO DATOS(datos, fecha) VALUES('Indagar si el m

and then again with the string

édico a tenido oportunidad de ...', '20/02/2013')

Therefore, you have to append the string in parser:foundCharacters: to the current string, e.g.

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [currentElementValue appendString:string];
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...