ios - XML 解析以获取链接,在 Objective-C 中

标签 ios objective-c xml parsing

我知道 XML 解析在论坛上已经解释了很多次,但是我有一个 XML 数据,我无法在我的 Objective-C 项目中解析它。

以下是我的 XML:

<cimpleML xmlns="http://www.example.org/cimple">
<Analytics segmentDelay="15000" serverUrl="http://adjingo.2cimple.com/adserver/analytics.xml" id="1"/>
<Campaign videoId="http://adjingo.s3.amazonaws.com/151/Video/6337.flv" publisherId="151" id="4151"/>
<Template orientation="vertical" bgGradientAlphas="" bgGradientRatio="0.5" bgGradientColors="0" bgColor="0" height="435.0" width="470.0" id="4375">
<div borderAlpha="1.0" borderColor="0" bgColor="0xffffff" splashResize="false" splashImage="" alpha="1.0" height="435.0" width="470.0" y="0.0" x="0.0" id="4352"/>
<videoCanvas actual_width="0.0" actual_height="0.0" bgColor="0" height="235.0" width="460.0" y="5.0" x="4.0" id="0"/>
<girgit>
<adunit transitionEffectDuration="1.0" transitionEffect="alpha" showOnce="false" duration="10.0" id="9392">
<displayApp panelType="normal" panelActivation="null" panel="right" divId="4352">
<renderingData id="6832">
<![CDATA[<?xml version="1.0" encoding="UTF-8"?><application backgroundAlpha="0" backgroundColor="0xD6EDFF" backgroundGradientAlpha="[]" backgroundGradientColor="[]" height="295" width="211"><image id="12771" click="getURL('http://adjingo.2cimple.com/adserver/urlmap/redirectUrl/ac0cb032-4144-48b0-b4d9-aa5d9944a743?campId=4151')" height='435.0' width='470.0' x='0.0' y='0.0' source='http://adjingo.2cimple.com/content/151/Image/6291.jpg' ><keyframes><keyframe x="0.0" y="0.0" width="470.0" height="435.0" alpha="1.0" framenumber="0" /></keyframes></image></application>]]>
</renderingData>
</displayApp>
</adunit>
<adunit transitionEffectDuration="1.0" transitionEffect="alpha" showOnce="false" duration="10.0" id="9393">
<displayApp panelType="normal" panelActivation="null" panel="right" divId="4352">
<renderingData id="6833">
<![CDATA[<?xml version="1.0" encoding="UTF-8"?><application backgroundAlpha="0" backgroundColor="0xD6EDFF" backgroundGradientAlpha="[]" backgroundGradientColor="[]" height="295" width="211"><image id="12772" click="getURL('http://adjingo.2cimple.com/adserver/urlmap/redirectUrl/ac0cb032-4144-48b0-b4d9-aa5d9944a743?campId=4151')" height='435.0' width='470.0' x='0.0' y='0.0' source='http://adjingo.2cimple.com/content/151/Image/6290.jpg' ><keyframes><keyframe x="0.0" y="0.0" width="470.0" height="435.0" alpha="1.0" framenumber="0" /></keyframes></image></application>]]>
</renderingData>
</displayApp>
</adunit>
</girgit>
<panels/>
</Template>
<Skin url="http://adjingo.2cimple.com/cimple/assets/Skin.swf"/>
<Ads/>
</cimpleML>

我其实想要在videoId=source=之后写的视频和图片链接,我想在我的屏幕上显示它们。但是,根据我的尝试,我无法获取所需的数据。

这是我试过的:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

    // If the current element name is equal to "cimpleML" then initialize the temporary dictionary.
    if ([elementName isEqualToString:@"cimpleML"]) {   
        self.dictTempDataStorage = [[NSMutableDictionary alloc] init];
    }

    // Keep the current element.
    self.currentElement = elementName;
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    if ([elementName isEqualToString:@"renderingData"]) { 
        // If the closing element equals to "renderingData" then the all the data inside it has been parsed and the dictionary should be added to the data array.
        [self.arrData addObject:[[NSDictionary alloc] initWithDictionary:self.dictTempDataStorage]];
    }
    else if ([elementName isEqualToString:@"adunit"]){
        // If the adunit element was found then store everything inside it.
        [self.dictTempDataStorage setObject:[NSString stringWithString:self.foundValue] forKey:@"adunitData"];
    }
}

请帮助我走上正轨。非常感谢您抽出宝贵时间。

最佳答案

您可能会使事情复杂化。 XML 解析器解析顶级元素。所以只要注意 CampaignCDataBlock 需要单独解析,因为它作为 NSData 对象出现。因此递归代码。

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    NSLog(@"%@", elementName);
    if([elementName isEqualTo:@"Campaign"])
    {
        NSString *videoId = [attributeDict objectForKey:@"videoId"];
        NSLog(@"video id is %@", videoId);
    }
    if([elementName isEqualTo:@"image"])
    {
        NSString *imageSource = [attributeDict objectForKey:@"source"];
        NSLog(@"image source is %@", imageSource);

    }
}

-(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    NSXMLParser *cDataParser = [[NSXMLParser alloc] initWithData:CDATABlock];
    cDataParser.delegate = self;
    [cDataParser parse];
}

关于ios - XML 解析以获取链接,在 Objective-C 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30714976/

相关文章:

ios - ABRecordCopyValue() EXC_BAD_ACCESS Error while getting kABPersonFirstNameProperty

c# - 转换过程中的 XSLT 问题

xml - 节点之间的 xPath

ios - 检测照片库为空

ios - 带有自动反转的 UIView 动画

ios - SDWebImage 滚动时清除图像缓存?

ios - 从 NSArray 分配给 NSMutableString 的不兼容指针类型

ios - XIB 文件和模拟器之间没有关联

ios - vDSP 和 FFT 计算

python - 用 Beautiful Soup 解析 XML