开发者

Displaying the contents of the xml page

开发者 https://www.devze.com 2022-12-17 20:25 出处:网络
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.T

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 开发者_JAVA百科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


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.


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.


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号