1
votes

I am trying to read a xml-file with internal html to show in a UIWebView.

I am using NSXMLParser to parse an incoming XML-file. Works like charm. However now I want to parse HTML that is included in the tags.

For instance this is parsed just fine:

<item>
   <letter>a</letter>
   <word>word 1</word>
   <description>Yada yada</description>
</item>

However this is making the parser crash:

<item>
   <letter>a</letter>
   <word>word 1</word>
   <description><p>Yada</p></description>
</item>

since the parser thinks it should translate

as xml-nodes obviously.

How could I change my parsing to handle internal html?

The code used in my class to parse the xml handles these nodes on method foundCharacters

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    // save the characters for the current item...
    if ([currentElement isEqualToString:@"letter"]) {
        [currentLetter appendString:string];
    } else if ([currentElement isEqualToString:@"word"]) {
        [currentWord appendString:string];
    } else if ([currentElement isEqualToString:@"description"]) {
        [currentDescription appendString:string];
    }

}

And this on didEndElement

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
    if ([elementName isEqualToString:@"item"]) {
        // save values to an item, then store that item into the array...
        [item setObject:currentLetter forKey:@"letter"];
        [item setObject:currentWord forKey:@"word"];
        [item setObject:currentDescription forKey:@"description"];

        [stories addObject:[item copy]];
    }

}
1
"How could I change my parsing" – How can we tell if you did not show any parsing code? And how do you define "internal HTML"? The second example is valid XML.Martin R
@MartinR Se my edited notes. I don't want the parser to handle <p> as an xml-node but it does :)Joakim M
As far as I know, there is no way to make NSXMLParser skip parsing and to treat certain XML elements as "normal text".Martin R
@MartinR wouldn't a CDATA block do just that?Daij-Djan
@JoakimM: That information would also have been useful in your question :)Martin R

1 Answers

2
votes

Wrap the content of the xml elements in CDATA blocks -- that way the parser will not see that as xml and skip it. (don't confuse it with PCDATA! Parsed CDATA won't help, but CDATA itself should do the trick)

you could then use a 2nd parser to parse that 'internal html'