1

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]];
    }

}
8
  • 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. Commented Mar 6, 2015 at 9:28
  • @MartinR Se my edited notes. I don't want the parser to handle <p> as an xml-node but it does :) Commented Mar 6, 2015 at 9:44
  • As far as I know, there is no way to make NSXMLParser skip parsing and to treat certain XML elements as "normal text". Commented Mar 6, 2015 at 9:50
  • @MartinR wouldn't a CDATA block do just that? Commented Mar 6, 2015 at 10:00
  • 2
    @JoakimM: That information would also have been useful in your question :) Commented Mar 6, 2015 at 10:15

1 Answer 1

2

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'

Sign up to request clarification or add additional context in comments.

3 Comments

So I would have <description><![CDATA[ ]]><p>Yada</p>]]></description> in the xml?
close: <![CDATA[<p>yada</p>]]> (and if you really need you could even do that on client side by preprocessing the data before parsing it -> but that would mean CPU time & memory on client side and should be avoided!)
Thanks! Will try it out tonight and mark your answer as the solution if it works out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.