用于在Objective-C

时间:2015-06-08 16:56:27

标签: 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"];
    }
}

请帮助我走上正轨。非常感谢你的时间。

1 个答案:

答案 0 :(得分:1)

你可能会使事情复杂化。 XML解析器解析顶级元素。所以只需注意{action}Campaign需要单独解析,因为它以CDataBlock对象的形式出现。因此,递归代码。

NSData