views:

156

answers:

3

I am new to iphone development.I want to parse an xml page .The source code contains some htmls tags.This html tag is displayed in my simulator.I want to filter the tags and display only the content.The sorce code of xml is like

           <description> 
            <![CDATA[<br /><p class="author"><span class="by">By: </span>By Sydney Ember</p><br><p>In the week since an earthquake devastated Haiti ...</p>]]>
            </description>

I want "in the week since an ..." to be displayed and not the html tags.Please help me out.Thanks

+1  A: 

I see that all the tags are HTML. In addition, there is a CDATA that defines that its content should be considered as text and not XML. As for the XML parsing - there are few XML parsers available for iPhone:

  • TouchXML
  • XPathQuery

I prefer the latter. I'm not sure how the parsers will treat the CDATA. Maybe you will have to parse twice - first time for getting the CDATA contents and second time for parsing the content...

Michael Kessler
Thanks for your answer
Warrior
+2  A: 

The contents inside a CDATA block are considered as text (xml specific chars like <, &, > etc will be ignored and treated as plain chars). If the text canvas you're using to display the text accepts html, read the text node of description tag and assign it to the innerHTML equivalent of the canvas.

Amarghosh
Thanks for your answer
Warrior
+2  A: 

Hi,

As said before in other answers, the data in your xml is inside a CDATA block - this means that when you get the contents of the tag, the XML parser won't be able to get rid of the 'By:' bit for you - as far as it's concerned, it's all just text.

However,if you're going to display it inside as HTML inside a UIWebView (instead of a UILabel etc), you can add a style sheet to the start of the string that makes the 'By:' hidden. Something like

NSString *cssString = @"<style type='text/css'>span.by { display:none; }</style>"
NSString *html = [NSString stringWithFormat:@"<html><head>%@</head><body>%@</body></html>", cssString, descriptionString];
[webView loadHTMLString:html baseURL:nil];

where descriptionString is the contents of the <description> tag in your xml.

However this approach is a little heavy handed, I would try very hard to get some cleaner xml from your server!

As for actually parsing the xml, try the NSXMLParser object - (random tutorial here)

Sam

deanWombourne
Thanks for your answer
Warrior