Iphone 要在XML解析后提取URl的一部分?
我试图解析一个XML文件,其中名为“description”的元素如下所示:Iphone 要在XML解析后提取URl的一部分?,iphone,objective-c,xml,Iphone,Objective C,Xml,我试图解析一个XML文件,其中名为“description”的元素如下所示: <description> <![CDATA[ <a href='http://www.okmagazine.com/posts/view/13756/'> <img src='http://www.okmagazine.com/img/photos/thumbs/27044' /> </a>
<description>
<![CDATA[
<a href='http://www.okmagazine.com/posts/view/13756/'>
<img src='http://www.okmagazine.com/img/photos/thumbs/27044' />
</a>
<br />
Ashlee and Pete take their tiny tot to FAO Schwarz in NYC for some new toys.
<p> <strong>Pete Wentz</strong> and <strong>Ashlee Simpson Wentz</strong> made the new parent pilgrimage to New York’s FAO Schwarz today, where 6-month old <strong>Bronx Mowgli </strong>was the...]]>
</description>
请帮忙
问候
Arun我从来没有使用过这个确切的框架,但您必须记住的是,虽然它在找到CDATA时会通知您,但对于解析器来说,里面的任何内容都只是纯文本。所以看起来您想要实现。您将获得一个NSData块,从那里您必须解析内容。现在,您可以使用另一个解析器来实现这一点,但只需手动执行子字符串可能会更快。您考虑过使用吗?
NSString*str=@Ashlee和Pete带着他们的小孩去纽约的FAO Schwarz买一些新玩具 Pete Wentz和Ashlee Simpson Wentz让新父母前往纽约朝圣™今天的施瓦茨,6个月大的布朗克斯·莫格利(Bronx Mowgli)是……]]>”;
NSRange range=[str rangeOfString:@“属性作为由属性名称键控的字符串字典传递给解析器的didStartElement委托方法。因此,您可以使用NSDictionary的objectForKey:以属性名称为键从属性中提取所需的URL。即:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName compare: @"img"] == NSOrderedSame) // check for <img ...> element
{
NSString* url = [attributeDictionary objectForKey:@"src"];
// url now contains the url you require from the HTML
-(void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName属性:(NSDictionary*)attributeDict
{
if([elementName比较:@“img”]==SensorDeredName)//检查Hey buddy..谢谢你的帮助..我会通知你你的想法是否有效或任何相关疑问再次感谢ArunHey buddy..谢谢你的帮助..我会通知你你的想法是否有效或任何相关疑问再次感谢ArunHey buddy..谢谢你的帮助..我会通知你的想法是否有效或任何相关d再次感谢你,阿鲁尼希望你能这样做。为了将来的参考,粘贴回复不如实际投票并选择一个正确的答案那么有用。更好的是,如果你的解决方案与建议的某个答案不同,就发布你的解决方案。因此,这不仅仅是回答你的问题,而是建立一个问题的权威数据库nd回答。嘿,伙计。谢谢你的帮助…我会告诉你你的想法是否有效或任何相关的疑问。再次感谢阿伦
NSString *str = @"<![CDATA[<a href='http://www.okmagazine.com/posts/view/13756/'><img src='http://www.okmagazine.com/img/photos/thumbs/27044' /></a><br />Ashlee and Pete take their tiny tot to FAO Schwarz in NYC for some new toys. <p> <strong>Pete Wentz</strong> and <strong>Ashlee Simpson Wentz</strong> made the new parent pilgrimage to New York’s FAO Schwarz today, where 6-month old <strong>Bronx Mowgli </strong>was the...]]>";
NSRange range = [str rangeOfString: @"<img src='"];
str = [str substringFromIndex: range.location + range.length];
range = [str rangeOfString: @"'"];
str =[str substringToIndex: range.location];
CFShow(str);
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName compare: @"img"] == NSOrderedSame) // check for <img ...> element
{
NSString* url = [attributeDictionary objectForKey:@"src"];
// url now contains the url you require from the HTML