0

I am currently using NSXML parser to parse my data. I can pass all data successfully accept an element who's values are "A, B, C depressor stick & D $10+".

I can only grab "D $10+".

I suspect it is because of this that is located under parser foundCharacters:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
//NSLog(@"foundCharacters: %@", string);    
currentNodeContent = (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

}

However, I have no idea on what to replace it that method with as I afraid that it might affect all the other elements that I could parse properly previously.

This is part of the output of the parsed document:

<title>Cleansing, gel, Tongue depressor stick &amp; gloves $10+</title>

The output of foundCharacters are as shown below

foundCharacters: Cleansing, gel,  Tongue depressor stick 
foundCharacters: &
foundCharacters:  gloves $10+

And I have appended the string under didEndElement:

[currentIssue.title appendString:currentNodeContent];
Confused_person
  • 103
  • 1
  • 2
  • 11
  • Can't understand! What is the value of "string" in the delegate function? Did you get the correct values? – Ramaraj T Aug 13 '13 at 04:55
  • As I mentioned earlier, the value is "A, B, C depressor stick & D $10+". But I only managed to grab "D $10+". – Confused_person Aug 13 '13 at 04:59
  • 1
    @Confused_person: Note that `foundCharacters` can be called more than once for one XML element, you have to *append* the strings. See e.g. http://stackoverflow.com/questions/14986699/nsxmlparser-dont-get-all-the-tag-if-the-tag-have-accent/14987201#14987201. – Martin R Aug 13 '13 at 05:04
  • Yes. I have also tried appending it under foundCharacters. I also tried appending it under didEndElement with no success. The problem is, it only grab the contents after "&" currently for this element. – Confused_person Aug 13 '13 at 05:10
  • @Confused_person: What does the original XML look like? Is the "&" properly escaped as `&`? – Martin R Aug 13 '13 at 05:14
  • I am not sure what do you mean. But the original parsed xml data contains "&amp". – Confused_person Aug 13 '13 at 05:19
  • Uncomment your NSLog statement and show the output. – Martin R Aug 13 '13 at 05:25
  • Here is part of the output of the parsed document, "Cleansing, gel, Tongue depressor stick & gloves $10+" – Confused_person Aug 13 '13 at 05:27
  • What I meant is: Add the the output of the `NSLog(@"foundCharacters: %@", string);` statement to your question. - You probably have to show a complete XML input and all your code in order to get help, because the "&" character (escaped as `&`) normally does not make any problems with NSXMLParser. – Martin R Aug 13 '13 at 05:41
  • Btw: How is this different from your (similar looking) previous question http://stackoverflow.com/questions/18179258/why-does-this-not-parse-correctly, which you solved yourself? – Martin R Aug 13 '13 at 05:42
  • Previous question was more about initializing issues to get the correct count of objects. – Confused_person Aug 13 '13 at 05:44
  • @Confused_person: You make it difficult to help! You said that you tried appending the strings in foundCharacters, but you don't show the code. I asked for the output of `NSLog(@"foundCharacters: %@", string);`, but you don't show it. - Without providing more concrete information, this will probably be closed as "duplicate" (which it might or might not be) or as "unclear what you are asking". – Martin R Aug 13 '13 at 06:53
  • Sorry. I was unsure of your request previously. Updated. – Confused_person Aug 13 '13 at 06:58
  • @Confused_person: Your NSLog output shows that it is exactly as I said: foundCharacters is called multiple times for the XML element, and you have to collect and append the strings. - (Now voting as "duplicate".) – Martin R Aug 13 '13 at 07:16
  • I have already appended it as I mentioned before under didEndElement. "[something.title appendString:currentNodeContent];" – Confused_person Aug 13 '13 at 07:45
  • @Confused_person: Your NSLog output shows clearly that `foundCharacters` is called tree times: First time with " Cleansing, gel, Tongue depressor stick", second time with "&", and finally with " gloves $10+". You have to append in `foundCharacters`, not in `didEndElement`! – Martin R Aug 13 '13 at 08:12
  • Yes I have tried that with no success. However, when I changed the initialization to "initWithString:string", I can see all the data but everything goes haywire after that. – Confused_person Aug 13 '13 at 08:53

1 Answers1

0

Just check the node name and in didfound characters,for that particular node do not trim the node value.

NHS
  • 409
  • 2
  • 7
  • Yes I have did that. It still does not store whatever contents parsed before &. – Confused_person Aug 13 '13 at 08:05
  • the ivar currentNodeContent is having some problem.. Dont use mutablestring make it a string and don't set it to nil anywhere. – NHS Aug 13 '13 at 08:54
  • 1. I changed it to "initWithString:string. 2. I appended it under foundCharacters. 3. I assigned the currentNodeContent to an element under didEndElement, then set it to nil. It works after that. – Confused_person Aug 14 '13 at 02:18