【问题标题】:XML Parsing to fetch links, in Objective-C用于获取链接的 XML 解析,在 Objective-C 中
【发布时间】:2015-06-08 16:56:27
【问题描述】:

我知道 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"];
    }
}

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

【问题讨论】:

    标签: ios objective-c xml parsing


    【解决方案1】:

    您可能会使事情复杂化。 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];
    }
    

    【讨论】:

    • 感谢@lead_the_zeppelin!我理解你的代码,除了为什么foundCDATA没有被调用?任何的想法?用断点检查。应用程序未进入此功能
    • @SHA 你确认它不是拼写错误吗?在将代码与您的确切 xml 文件粘贴到此处之前,我运行了代码。
    • 是的,它与我复制的完全相同。事实上,foundCharacters 方法正在被调用,它一个一个地给出单个字符,这显然不是我需要的。我还在评论here 中读到它有时会调用foundCharacters 而不是foundCDATA.. 困惑
    • 是的@lead_the_zeppelin,据我所知,它是UTF-8格式
    • 对不起,我不知道发生了什么。看起来像一个错误。
    猜你喜欢
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    相关资源
    最近更新 更多